Skip to content

Commit

Permalink
remove any tape/qtape property calls
Browse files Browse the repository at this point in the history
  • Loading branch information
andrijapau committed Nov 14, 2024
1 parent 3370db1 commit 90f8651
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
47 changes: 12 additions & 35 deletions demonstrations/qnspsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_apply_qsvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ 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))

###############################################################################
# We can inspect details by drawing the expanded circuit. The :class:`~.pennylane.QSVT`
# 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.
Expand Down
3 changes: 2 additions & 1 deletion demonstrations/tutorial_clifford_circuit_simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit 90f8651

Please sign in to comment.