qp.change_op_basis

change_op_basis(compute_op, target_op, uncompute_op=None)[source]

Construct an operator representing a compute-uncompute pattern.

Parameters:
  • compute_op (Operator or Callable) – operator applied before the target

  • target_op (Operator or Callable) – operator applied in the changed basis

  • uncompute_op (Operator or Callable or None) – Operator applied after the target. If None (the default), the adjoint of compute_op is used.

Returns:

the operator representing the compute-uncompute pattern

Return type:

ChangeOpBasis or ChangeOpBasis2

Raises:

TypeError – if an operand is not an operator or a supported callable

Note

With program capture enabled, all operands must be operators. Use subcircuit() to turn a DecompositionRule into an operator class, then pass an instance of that class.

With program capture disabled, callables with no unbound mandatory parameters are also accepted (see below for details).

Example

Consider the following example involving a change_op_basis. The compute-uncompute pattern is composed of a Quantum Fourier Transform (QFT), followed by a PhaseAdder, and finally an inverse QFT.

import pennylane as qp
from functools import partial

dev = qp.device("default.qubit")
@qp.qnode(dev)
def circuit():
    qp.H(0)
    qp.CNOT([1,2])
    qp.ctrl(
        qp.change_op_basis(qp.QFT([1,2]), qp.PhaseAdder(1, x_wires=[1,2])),
        control=0
    )
    return qp.state()

circuit2 = qp.transforms._tape_decompose(circuit, gate_set=qp.gate_sets.ROTATIONS_PLUS_CNOT, max_expansion=1)

When this circuit is decomposed, the compute_op and uncompute_op are not controlled, resulting in a much more resource-efficient decomposition:

>>> print(qp.draw(circuit2)())
0: ──RZ(3.14)──RY(1.57)─╭GlobalPhase(-1.57)─────────╭●──────────────────────┤ ╭State
1: ─────────────────────├GlobalPhase(-1.57)─╭●─╭QFT─├PhaseAdder(1.00)─╭QFT†─┤ ├State
2: ─────────────────────╰GlobalPhase(-1.57)─╰X─╰QFT─╰PhaseAdder(1.00)─╰QFT†─┤ ╰State

With program capture disabled, a callable can also be provided as an argument. The callable applies a series of operations and must have no unbound mandatory parameters. functools.partial can be used to bind any necessary parameters.

def my_compute_op(a, reg1, reg2):
    qp.BasisState(np.zeros(len(reg2)), reg2)
    qp.QFT(reg1)
    qp.RX(a, reg1[0])

def my_target_op(wires):
    qp.PauliX(wires[0])

dev = qp.device("default.qubit")

@qp.qnode(dev)
def circuit():
    # Use partial to absorb any input parameters
    compute = partial(my_compute_op, 0.1, [0], [1])
    target = partial(my_target_op, [0])
    qp.change_op_basis(compute, target)
    return qp.state()

circuit3 = qp.transforms._tape_decompose(circuit, gate_set=qp.gate_sets.ROTATIONS_PLUS_CNOT, max_expansion=1)
>>> print(qp.draw(circuit3)())
0: ─╭RX(0.10)@QFT@|Ψ⟩──X─╭(RX(0.10)@QFT@|Ψ⟩)†─┤ ╭State
1: ─╰RX(0.10)@QFT@|Ψ⟩────╰(RX(0.10)@QFT@|Ψ⟩)†─┤ ╰State

Instantiate a subcircuit() to use a quantum function as an operand:

@qp.subcircuit(dynamic_argnames=("angle",))
@qp.register_resources({qp.H: 1, qp.RX: 1})
def Compute(angle, wires):
    qp.H(wires)
    qp.RX(angle, wires)

qp.capture.enable()
op = qp.change_op_basis(Compute(0.2, wires=0), qp.RZ(0.3, 0))
>>> isinstance(op, qp.ops.ChangeOpBasis2)
True
>>> qp.capture.disable()