From 90f8651b6054ddee51cd90c4aca88554c822cb56 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 14 Nov 2024 12:48:10 -0500 Subject: [PATCH] remove any tape/qtape property calls --- demonstrations/qnspsa.py | 47 +++++-------------- demonstrations/tutorial_apply_qsvt.py | 4 +- .../tutorial_clifford_circuit_simulations.py | 3 +- 3 files changed, 16 insertions(+), 38 deletions(-) diff --git a/demonstrations/qnspsa.py b/demonstrations/qnspsa.py index d859f176e0..da5ad1b6a9 100644 --- a/demonstrations/qnspsa.py +++ b/demonstrations/qnspsa.py @@ -358,22 +358,17 @@ def get_grad(params_curr): from copy import copy - -def get_operations(qnode, params): - qnode.construct([params], {}) - return qnode.tape.operations - - def get_overlap_tape(qnode, params1, params2): - op_forward = get_operations(qnode, params1) - op_inv = get_operations(qnode, params2) + tape_forward = qml.workflow.construct_tape(qnode)(*params1) + tape_inv = qml.workflow.construct_tape(qnode)(*params2) + + ops = tape_forward.operations + list(qml.adjoint(copy(op)) for op in reversed(tape_inv.operations)) with qml.tape.QuantumTape() as tape: - for op in op_forward: + for op in ops: qml.apply(op) - for op in reversed(op_inv): - qml.adjoint(copy(op)) - qml.probs(wires=qnode.tape.wires.labels) + qml.probs(wires=tape_forward.wires.labels) + return tape @@ -833,10 +828,8 @@ def __get_spsa_grad_tapes(self, cost, params): # used to estimate the gradient per optimization step. The sampled # direction is of the shape of the input parameter. direction = self.__get_perturbation_direction(params) - cost.construct([params + self.finite_diff_step * direction], {}) - tape_forward = cost.tape.copy(copy_operations=True) - cost.construct([params - self.finite_diff_step * direction], {}) - tape_backward = cost.tape.copy(copy_operations=True) + tape_forward = qml.workflow.construct_tape(cost)(*[params + self.finite_diff_step * direction]) + tape_backward = qml.workflow.construct_tape(cost)(*[params - self.finite_diff_step * direction]) return [tape_forward, tape_backward], direction def __update_tensor(self, tensor_raw): @@ -864,21 +857,7 @@ def __get_tensor_tapes(self, cost, params): return tapes, dir_vecs def __get_overlap_tape(self, cost, params1, params2): - op_forward = self.__get_operations(cost, params1) - op_inv = self.__get_operations(cost, params2) - - with qml.tape.QuantumTape() as tape: - for op in op_forward: - qml.apply(op) - for op in reversed(op_inv): - qml.adjoint(copy(op)) - qml.probs(wires=cost.tape.wires.labels) - return tape - - def __get_operations(self, cost, params): - # Given a QNode, returns the list of operations before the measurement. - cost.construct([params], {}) - return cost.tape.operations + return get_overlap_tape(cost, params1, params2) def __get_tensor_moving_avg(self, metric_tensor): # For numerical stability: averaging on the Fubini-Study metric tensor. @@ -893,10 +872,8 @@ def __regularize_tensor(self, metric_tensor): def __apply_blocking(self, cost, params_curr, params_next): # For numerical stability: apply the blocking condition on the parameter update. - cost.construct([params_curr], {}) - tape_loss_curr = cost.tape.copy(copy_operations=True) - cost.construct([params_next], {}) - tape_loss_next = cost.tape.copy(copy_operations=True) + tape_loss_curr = qml.workflow.construct_tape(cost)(*[params_curr]) + tape_loss_next = qml.workflow.construct_tape(cost)(*[params_next]) loss_curr, loss_next = qml.execute([tape_loss_curr, tape_loss_next], cost.device, None) # self.k has been updated earlier. diff --git a/demonstrations/tutorial_apply_qsvt.py b/demonstrations/tutorial_apply_qsvt.py index b441ba0c8f..b27da84bef 100644 --- a/demonstrations/tutorial_apply_qsvt.py +++ b/demonstrations/tutorial_apply_qsvt.py @@ -55,7 +55,7 @@ def my_circuit(phase_angles): ############################################################################### # We can now execute the circuit and visualize it. -my_circuit(phase_angles) +tape = qml.workflow.construct_tape(my_circuit)(phase_angles) print(qml.draw(my_circuit)(phase_angles)) ############################################################################### @@ -63,7 +63,7 @@ def my_circuit(phase_angles): # operation is composed of repeated applications of the :class:`~.pennylane.BlockEncode` and # :class:`~.pennylane.PCPhase` (:math:`\Pi_{\phi}`) operations. -print(my_circuit.tape.expand().draw()) +print(tape.expand().draw()) ############################################################################### # Now let's look at an application of QSVT --- solving a linear system of equations. diff --git a/demonstrations/tutorial_clifford_circuit_simulations.py b/demonstrations/tutorial_clifford_circuit_simulations.py index 60eef7a1e4..acc2402425 100644 --- a/demonstrations/tutorial_clifford_circuit_simulations.py +++ b/demonstrations/tutorial_clifford_circuit_simulations.py @@ -381,7 +381,8 @@ def state_at_each_step(tape): # Let's examine the remaining operations to confirm this. # -circuit_ops = circuit.tape.operations +tape = qml.workflow.construct_tape(circuit)() +circuit_ops = tape.operations print("Circ. Ops: ", circuit_ops) for step in range(1, len(circuit_ops)):