Skip to content
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

Clean up QuantumScript._par_info #3185

Merged
merged 47 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
39037be
move tape properties and methods to quantumscript class
albi3ro Sep 21, 2022
d270140
fix adjoint test
albi3ro Sep 21, 2022
e5f017b
update documentation
albi3ro Sep 21, 2022
2d4f7f0
Merge branch 'master' into qscript
albi3ro Sep 21, 2022
fdc4f7e
move to tape module
albi3ro Sep 21, 2022
f803132
changelog
albi3ro Sep 21, 2022
2126024
Merge branch 'master' into qscript
albi3ro Sep 21, 2022
f8fb739
fix test
albi3ro Sep 22, 2022
fcc8c75
Merge branch 'master' into qscript
albi3ro Sep 22, 2022
065213a
docstring fix'
albi3ro Sep 22, 2022
1637771
more tests
albi3ro Sep 26, 2022
1264cd8
Apply suggestions from code review
albi3ro Oct 3, 2022
7181fa4
Merge branch 'master' into qscript
albi3ro Oct 4, 2022
c61b161
Merge branch 'master' into qscript
albi3ro Oct 7, 2022
3b29af8
misc improvements
albi3ro Oct 11, 2022
767e83c
more tests
albi3ro Oct 12, 2022
3cb5e23
Merge branch 'master' into qscript
albi3ro Oct 12, 2022
200efa1
remove trailing whitespace
albi3ro Oct 12, 2022
d99d6bd
Apply suggestions from code review
albi3ro Oct 12, 2022
2b9f9ac
Apply suggestions from code review
albi3ro Oct 12, 2022
64ea29a
responding to feedback
albi3ro Oct 12, 2022
a191c26
hope i did merge rigth
albi3ro Oct 12, 2022
9e404f1
oops
albi3ro Oct 12, 2022
d1184dd
Merge branch 'master' into qscript
albi3ro Oct 13, 2022
11de7ff
Merge branch 'master' into qscript
albi3ro Oct 13, 2022
23d36ae
updating numeric_type and shape
albi3ro Oct 13, 2022
693c37e
Merge branch 'qscript' of https://github.com/PennyLaneAI/pennylane in…
albi3ro Oct 13, 2022
867c6a7
fix test
albi3ro Oct 13, 2022
4c018b0
Update doc/releases/changelog-dev.md
albi3ro Oct 14, 2022
76493d0
merge with master
albi3ro Oct 14, 2022
6582701
Merge branch 'qscript' of https://github.com/PennyLaneAI/pennylane in…
albi3ro Oct 14, 2022
348c2b2
remove example from changelog
albi3ro Oct 14, 2022
c665e3b
Merge branch 'master' into qscript
albi3ro Oct 14, 2022
c023a86
Merge branch 'master' into qscript
albi3ro Oct 18, 2022
5104e88
Merge branch 'master' into qscript
albi3ro Oct 18, 2022
e2f36c4
clean up _par_info attribute
albi3ro Oct 18, 2022
fb27cbd
Update doc/releases/changelog-dev.md
albi3ro Oct 18, 2022
1af83bf
Merge branch 'master' into par_info_list
albi3ro Oct 21, 2022
18a310a
Merge branch 'master' into par_info_list
AlbertMitjans Oct 25, 2022
f6efdcb
Update pennylane/tape/qscript.py
albi3ro Oct 25, 2022
de95f7b
Merge branch 'master' into par_info_list
albi3ro Oct 25, 2022
0f86036
fix docstring
albi3ro Oct 25, 2022
72efa6f
fix docstring
albi3ro Oct 25, 2022
2168499
fix docstring again
albi3ro Oct 25, 2022
8fb76ec
Update pennylane/tape/qscript.py
albi3ro Oct 25, 2022
5651d2a
bblack
albi3ro Oct 25, 2022
5fc88ef
retrigger
albi3ro Oct 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ keyword argument when using `GellMann`, which determines which of the 8 Gell-Man

<h3>Improvements</h3>

* Added a new `pennylane.tape.QuantumScript` class that contains all the non-queuing behavior of `QuantumTape`. Now `QuantumTape` inherits from `QuantumScript` as well
as `AnnotatedQueue`.
This is a developer-facing change, and users should not manipulate `QuantumScript` directly. Instead, they
should continue to rely on `QNode`s.
[(#3097)](https://github.com/PennyLaneAI/pennylane/pull/3097)

* `Adjoint` now supports batching if the base operation supports batching.
[(#3168)](https://github.com/PennyLaneAI/pennylane/pull/3168)

Expand Down Expand Up @@ -133,6 +139,8 @@ keyword argument when using `GellMann`, which determines which of the 8 Gell-Man

<h3>Breaking changes</h3>

* `QuantumTape._par_info` is now a list of dictionaries, instead of a dictionary whose keys are integers starting from zero.
albi3ro marked this conversation as resolved.
Show resolved Hide resolved

* `QueuingContext` is renamed `QueuingManager`.
[(#3061)](https://github.com/PennyLaneAI/pennylane/pull/3061)

Expand Down
7 changes: 3 additions & 4 deletions pennylane/circuit_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ class CircuitGraph:
ops (Iterable[.Operator]): quantum operators constituting the circuit, in temporal order
obs (Iterable[.MeasurementProcess]): terminal measurements, in temporal order
wires (.Wires): The addressable wire registers of the device that will be executing this graph
par_info (dict[int, dict[str, .Operation or int]]): Parameter information. Keys are
parameter indices (in the order they appear on the tape), and values are a
dictionary containing the corresponding operation and operation parameter index.
par_info (list[dict]): Parameter information. For each index, the entry is a dictionary containing an operation
and an index into that operation's parameters.
trainable_params (set[int]): A set containing the indices of parameters that support
differentiability. The indices provided match the order of appearence in the
quantum circuit.
Expand Down Expand Up @@ -420,7 +419,7 @@ def parametrized_layers(self):
current = Layer([], [])
layers = [current]

for idx, info in self.par_info.items():
for idx, info in enumerate(self.par_info):
if idx in self.trainable_params:
op = info["op"]

Expand Down
4 changes: 2 additions & 2 deletions pennylane/gradients/gradient_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def gradient_analysis(tape, use_graph=True, grad_fn=None):
if grad_fn is not None:
tape._gradient_fn = grad_fn

for idx, info in tape._par_info.items():
for idx, info in enumerate(tape._par_info):

if idx not in tape.trainable_params:
# non-trainable parameters do not require a grad_method
Expand Down Expand Up @@ -101,7 +101,7 @@ def grad_method_validation(method, tape):

diff_methods = {
idx: info["grad_method"]
for idx, info in tape._par_info.items() # pylint: disable=protected-access
for idx, info in enumerate(tape._par_info) # pylint: disable=protected-access
if idx in tape.trainable_params
}

Expand Down
2 changes: 1 addition & 1 deletion pennylane/gradients/parameter_shift_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _gradient_analysis_cv(tape):

tape._gradient_fn = param_shift_cv

for idx, info in tape._par_info.items():
for idx, info in enumerate(tape._par_info):
info["grad_method"] = _grad_method(tape, idx)


Expand Down
2 changes: 1 addition & 1 deletion pennylane/ops/qubit/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def __matmul__(self, H):
coeffs = qml.math.kron(coeffs1, coeffs2)
ops_list = itertools.product(ops1, ops2)
terms = [qml.operation.Tensor(t[0], t[1]) for t in ops_list]

print(coeffs, terms)
return qml.Hamiltonian(coeffs, terms, simplify=True)

if isinstance(H, (Tensor, Observable)):
Expand Down
4 changes: 2 additions & 2 deletions pennylane/optimize/lie_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def algebra_commutator(tape, observables, lie_algebra_basis_names, nqubits):
for obs in observables:
for o in obs:
# create a list of tapes for the plus and minus shifted circuits
tapes_plus = [qml.tape.QuantumTape(p + "_p") for p in lie_algebra_basis_names]
tapes_min = [qml.tape.QuantumTape(p + "_m") for p in lie_algebra_basis_names]
tapes_plus = [qml.tape.QuantumTape(name=f"{p}_p") for p in lie_algebra_basis_names]
tapes_min = [qml.tape.QuantumTape(name=f"{p}_m") for p in lie_algebra_basis_names]

# loop through all operations on the input tape
for op in tape.operations:
Expand Down
1 change: 1 addition & 0 deletions pennylane/tape/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

from .tape import QuantumTape, get_active_tape, TapeError
from .operation_recorder import OperationRecorder
from .qscript import QuantumScript
from .stop_recording import stop_recording
from .unwrap import Unwrap, UnwrapTape
13 changes: 7 additions & 6 deletions pennylane/tape/operation_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
This module contains the :class:`OperationRecorder`.
"""
# pylint: disable=too-many-arguments
from pennylane.queuing import QueuingManager

from .tape import QuantumTape
Expand Down Expand Up @@ -42,8 +43,12 @@ class OperationRecorder(QuantumTape):
objects.
"""

def __init__(self):
super().__init__()
def __init__(
self, ops=None, measurements=None, prep=None, name=None, do_queue=False, _update=True
):
super().__init__(
ops, measurements, prep=prep, name=name, do_queue=do_queue, _update=_update
)
self.ops = None
self.obs = None

Expand All @@ -53,10 +58,6 @@ def _process_queue(self):
for obj, info in self._queue.items():
QueuingManager.append(obj, **info)

# remove the operation recorder from the queuing
# context
QueuingManager.remove(self)

new_tape = self.expand(depth=5, stop_at=lambda obj: not isinstance(obj, QuantumTape))
self.ops = new_tape.operations
self.obs = new_tape.observables
Expand Down
Loading