qml.labs.zxopt.basic_optimization

basic_optimization(tape)[source]

Apply zx.basic_optimization to a PennyLane phase polynomial circuit with Hadamard gates. This step can help improve phase polynomial based optimization schemes like todd() or full_optimize() by moving Hadamard gates in order to create big and few phase polynomial blocks.

Parameters:

tape (QNode or QuantumTape or Callable) – Input PennyLane circuit.

Returns:

Improved PennyLane circuit. See qml.transform for the different output formats depending on the input type.

Return type:

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

See also

full_reduce() (arbitrary circuits), full_optimize() ((Clifford + T) circuits)

Example

This pass tries to push Hadamard gates as far as possible to the side to allow better phase polynomial optimization via, e.g., todd().

from pennylane.labs.zxopt import basic_optimization
circ = qml.tape.QuantumScript([
    qml.CNOT((0, 1)),
    qml.T(0),
    qml.CNOT((3, 2)),
    qml.Hadamard(0),
    qml.T(1),
    qml.Hadamard(1),
    qml.CNOT((1, 2)),
    qml.Hadamard(2),
    qml.T(2),
    qml.Hadamard(2),
    qml.RZ(0.5, 1),
    qml.CNOT((1, 2)),
    qml.T(1),
    qml.CNOT((3, 2)),
    qml.Hadamard(1),
    qml.T(0),
    qml.CNOT((0, 1)),
], [])

print(f"Circuit before:")
print(qml.drawer.tape_text(circ, wire_order=range(4)))

(new_circ,), _ = basic_optimization(circ)
print(f"Circuit after basic_optimization:")
print(qml.drawer.tape_text(new_circ, wire_order=range(4)))
Circuit before:
0: ─╭●──T──H──T────────────────────╭●─┤
1: ─╰X──T──H─╭●──RZ───────╭●──T──H─╰X─┤
2: ─╭X───────╰X──H───T──H─╰X─╭X───────┤
3: ─╰●───────────────────────╰●───────┤

Circuit after basic_optimization:
0: ──T─╭●───────────╭X──H──T─┤
1: ────╰X──T──H──RZ─╰●──H────┤
2: ─╭●───────╭Z──────────────┤
3: ─╰X──H──T─╰●──H───────────┤