-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add C++ adjoint diff methods #136
Changes from all commits
fbdeedd
a26cb1e
b0d49a2
adbc267
88a1595
ec9db68
d47eb2c
ca950b3
409759f
b955bee
140f950
cf07cc9
9e8ae62
aa19e98
35ba63d
edf6b1b
4ea6c3d
5859083
1d456fa
3e91d07
1a12eb9
2317a94
2216b51
f5e029a
ca706c2
81d41ba
5df74a0
9842bb4
f154807
09fb87d
75530bd
1cb2fcd
1743fc4
8f1a585
ebcf216
c1a7c27
bf181e1
09f9e4b
31864cc
224bc9a
1e85c63
ff4cee3
bf2de2a
3b10aca
ac01781
2756cdf
c705157
0cc4db3
26a9607
c02d21c
9cea641
707c0e8
30bd6b4
fa81fc5
ac8b857
101b31a
f1b38c6
3409828
11d62d4
ae5d41f
61ccffc
3626f2f
05bdacb
f95aa02
380135a
bdab848
e0c4006
2ae4d7e
cf5ac07
9003139
7c51f2c
e6f99d9
189a837
0441afd
04ee646
e1d569f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ env: | |
# MacOS specific build settings | ||
CIBW_BEFORE_ALL_MACOS: | | ||
brew uninstall --force oclint | ||
brew install gcc libomp | ||
brew install llvm libomp | ||
|
||
# Python build settings | ||
CIBW_BEFORE_BUILD: | | ||
|
@@ -53,6 +53,8 @@ jobs: | |
run: python -m cibuildwheel --output-dir wheelhouse | ||
env: | ||
CIBW_ARCHS_MACOS: ${{matrix.arch}} | ||
CC: /usr/local/opt/llvm/bin/clang | ||
CXX: /usr/local/opt/llvm/bin/clang++ | ||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the CC option for C (i.e., the language)? Do we need it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, and you are right. Removing CC should be fine (though I'll test it out). There may be a way to avoid use of this too, though I must check this out first. |
||
|
||
- uses: actions/upload-artifact@v2 | ||
if: github.ref == 'refs/heads/master' | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,148 @@ | ||||||||||
# Copyright 2021 Xanadu Quantum Technologies Inc. | ||||||||||
|
||||||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
# you may not use this file except in compliance with the License. | ||||||||||
# You may obtain a copy of the License at | ||||||||||
|
||||||||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
|
||||||||||
# Unless required by applicable law or agreed to in writing, software | ||||||||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
# See the License for the specific language governing permissions and | ||||||||||
# limitations under the License. | ||||||||||
r""" | ||||||||||
Helper functions for serializing quantum tapes. | ||||||||||
""" | ||||||||||
from typing import List, Tuple | ||||||||||
|
||||||||||
import numpy as np | ||||||||||
from pennylane import BasisState, Hadamard, Projector, QubitStateVector, Rot | ||||||||||
from pennylane.grouping import is_pauli_word | ||||||||||
from pennylane.operation import Observable, Tensor | ||||||||||
from pennylane.tape import QuantumTape | ||||||||||
|
||||||||||
try: | ||||||||||
from .lightning_qubit_ops import StateVectorC128, ObsStructC128 | ||||||||||
except ImportError: | ||||||||||
pass | ||||||||||
|
||||||||||
|
||||||||||
def _obs_has_kernel(obs: Observable) -> bool: | ||||||||||
"""Returns True if the input observable has a supported kernel in the C++ backend. | ||||||||||
|
||||||||||
Args: | ||||||||||
obs (Observable): the input observable | ||||||||||
|
||||||||||
Returns: | ||||||||||
bool: indicating whether ``obs`` has a dedicated kernel in the backend | ||||||||||
""" | ||||||||||
if is_pauli_word(obs): | ||||||||||
return True | ||||||||||
if isinstance(obs, (Hadamard, Projector)): | ||||||||||
return True | ||||||||||
if isinstance(obs, Tensor): | ||||||||||
return all(_obs_has_kernel(o) for o in obs.obs) | ||||||||||
return False | ||||||||||
|
||||||||||
|
||||||||||
def _serialize_obs(tape: QuantumTape, wires_map: dict) -> List: | ||||||||||
"""Serializes the observables of an input tape. | ||||||||||
|
||||||||||
Args: | ||||||||||
tape (QuantumTape): the input quantum tape | ||||||||||
wires_map (dict): a dictionary mapping input wires to the device's backend wires | ||||||||||
|
||||||||||
Returns: | ||||||||||
list(ObsStructC128): A list of observable objects compatible with the C++ backend | ||||||||||
""" | ||||||||||
obs = [] | ||||||||||
|
||||||||||
for o in tape.observables: | ||||||||||
is_tensor = isinstance(o, Tensor) | ||||||||||
|
||||||||||
wires = [] | ||||||||||
|
||||||||||
if is_tensor: | ||||||||||
for o_ in o.obs: | ||||||||||
wires_list = o_.wires.tolist() | ||||||||||
w = [wires_map[w] for w in wires_list] | ||||||||||
wires.append(w) | ||||||||||
else: | ||||||||||
wires_list = o.wires.tolist() | ||||||||||
w = [wires_map[w] for w in wires_list] | ||||||||||
wires.append(w) | ||||||||||
|
||||||||||
name = o.name if is_tensor else [o.name] | ||||||||||
|
||||||||||
params = [] | ||||||||||
|
||||||||||
if not _obs_has_kernel(o): | ||||||||||
if is_tensor: | ||||||||||
for o_ in o.obs: | ||||||||||
if not _obs_has_kernel(o_): | ||||||||||
params.append(o_.matrix.ravel().astype(np.complex128)) | ||||||||||
else: | ||||||||||
params.append([]) | ||||||||||
else: | ||||||||||
params.append(o.matrix.ravel().astype(np.complex128)) | ||||||||||
|
||||||||||
ob = ObsStructC128(name, params, wires) | ||||||||||
obs.append(ob) | ||||||||||
|
||||||||||
return obs | ||||||||||
|
||||||||||
|
||||||||||
def _serialize_ops( | ||||||||||
tape: QuantumTape, wires_map: dict | ||||||||||
) -> Tuple[List[List[str]], List[np.ndarray], List[List[int]], List[bool], List[np.ndarray]]: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this should be fine, I wonder will it mess with the conversion to C++ (though I'd imagine pybind is clever enough that we do need to be explicit). |
||||||||||
"""Serializes the operations of an input tape. | ||||||||||
|
||||||||||
The state preparation operations are not included. | ||||||||||
|
||||||||||
Args: | ||||||||||
tape (QuantumTape): the input quantum tape | ||||||||||
wires_map (dict): a dictionary mapping input wires to the device's backend wires | ||||||||||
|
||||||||||
Returns: | ||||||||||
Tuple[list, list, list, list, list]: A serialization of the operations, containing a list | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
of operation names, a list of operation parameters, a list of observable wires, a list of | ||||||||||
inverses, and a list of matrices for the operations that do not have a dedicated kernel. | ||||||||||
""" | ||||||||||
names = [] | ||||||||||
params = [] | ||||||||||
wires = [] | ||||||||||
inverses = [] | ||||||||||
mats = [] | ||||||||||
|
||||||||||
uses_stateprep = False | ||||||||||
|
||||||||||
for o in tape.operations: | ||||||||||
if isinstance(o, (BasisState, QubitStateVector)): | ||||||||||
uses_stateprep = True | ||||||||||
continue | ||||||||||
elif isinstance(o, Rot): | ||||||||||
op_list = o.expand().operations | ||||||||||
else: | ||||||||||
op_list = [o] | ||||||||||
|
||||||||||
for single_op in op_list: | ||||||||||
is_inverse = single_op.inverse | ||||||||||
|
||||||||||
name = single_op.name if not is_inverse else single_op.name[:-4] | ||||||||||
names.append(name) | ||||||||||
|
||||||||||
if getattr(StateVectorC128, name, None) is None: | ||||||||||
params.append([]) | ||||||||||
mats.append(single_op.matrix) | ||||||||||
|
||||||||||
if is_inverse: | ||||||||||
is_inverse = False | ||||||||||
else: | ||||||||||
params.append(single_op.parameters) | ||||||||||
mats.append([]) | ||||||||||
|
||||||||||
wires_list = single_op.wires.tolist() | ||||||||||
wires.append([wires_map[w] for w in wires_list]) | ||||||||||
inverses.append(is_inverse) | ||||||||||
return (names, params, wires, inverses, mats), uses_stateprep |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me this is a major new feature 🚀 🎉 We can probably move it up during release 😁