apply(op, context=<class 'pennylane.queuing.QueuingManager'>)¶
Apply an instantiated operator or measurement to a queuing context.
the input operator is returned for convenience
- Return type
In PennyLane, operations and measurements are ‘queued’ or added to a circuit when they are instantiated.
applyfunction can be used to add operations that might have already been instantiated elsewhere to the QNode:
op = qml.RX(0.4, wires=0) dev = qml.device("default.qubit", wires=2) @qml.qnode(dev) def circuit(x): qml.RY(x, wires=0) # applied during instantiation qml.apply(op) # manually applied return qml.expval(qml.PauliZ(0))
>>> print(qml.draw(circuit)(0.6)) 0: ──RY(0.6)──RX(0.4)──┤ ⟨Z⟩
It can also be used to apply functions repeatedly:
@qml.qnode(dev) def circuit(x): qml.apply(op) qml.RY(x, wires=0) qml.apply(op) return qml.expval(qml.PauliZ(0))
>>> print(qml.draw(circuit)(0.6)) 0: ──RX(0.4)──RY(0.6)──RX(0.4)──┤ ⟨Z⟩
If you use
applyon an operator that has already been queued, it will be queued for a second time. For example:
@qml.qnode(dev) def circuit(): op = qml.Hadamard(0) qml.apply(op) return qml.expval(qml.PauliZ(0))
>>> print(qml.draw(circuit)()) 0: ──H──H─┤ <Z>
Instantiated measurements can also be applied to queuing contexts using
meas = qml.expval(qml.PauliZ(0) @ qml.PauliY(1)) dev = qml.device("default.qubit", wires=2) @qml.qnode(dev) def circuit(x): qml.RY(x, wires=0) qml.CNOT(wires=[0, 1]) return qml.apply(meas)
>>> print(qml.draw(circuit)(0.6)) 0: ──RY(0.6)──╭●──╭┤ ⟨Z ⊗ Y⟩ 1: ───────────╰X──╰┤ ⟨Z ⊗ Y⟩
applywill queue operators to the currently active queuing context.
When working with low-level queuing contexts such as quantum tapes, the desired context to queue the operation to can be explicitly passed:
with qml.tape.QuantumTape() as tape1: qml.Hadamard(wires=1) with qml.tape.QuantumTape() as tape2: # Due to the nesting behaviour of queuing contexts, # tape2 will be queued to tape1. # The following PauliX operation will be queued # to the active queuing context, tape2, during instantiation. op1 = qml.PauliX(wires=0) # We can use qml.apply to apply the same operation to tape1 # without leaving the tape2 context. qml.apply(op1, context=tape1) qml.RZ(0.2, wires=0) qml.CNOT(wires=[0, 1])
>>> tape1.operations [Hadamard(wires=), <QuantumTape: wires=, params=1>, PauliX(wires=), CNOT(wires=[0, 1])] >>> tape2.operations [PauliX(wires=), RZ(0.2, wires=)]
- What is PennyLane?
- Quantum circuits
- Gradients and training
- Quantum operators
- Inspecting circuits
- Compiling circuits
- Quantum Chemistry
- Quantum Datasets
- QNode returns