qml.transforms.match_controlled_iX_gate

match_controlled_iX_gate(tape, num_controls=1)[source]

Quantum transform to replace controlled iX gates. An iX gate is a controlled-S and a Toffoli. The equivalency used is given in Giles, Brett, and Peter Selinger. “Exact Synthesis of Multiqubit Clifford+T Circuits”, arXiv:1212.0506, arXiv, 2013. doi:10.48550/arXiv.1212.0506.

Note

Will also replace any subcircuits from the full pattern (composed of the controlled iX gate and its decomposition) that can replaced by the rest of the pattern.

Parameters:
  • tape (QNode or QuantumScript or Callable) – A quantum circuit.

  • num_controls (int) – The number of controls on the CS gate.

Returns:

The transformed circuit as described in qml.transform.

Return type:

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

Example

Consider the following quantum function:

def qfunc():
    qml.ctrl(qml.S(wires=[2]), control=[0, 1])
    qml.MultiControlledX(wires=[0, 1, 2, 3])
    return qml.expval(qml.Z(0))

The circuit (a controlled iX gate) before decomposition:

>>> dev = qml.device('default.qubit', wires=4)
>>> qnode = qml.QNode(qfunc, dev)
>>> lowered_qnode = match_controlled_iX_gate(qnode, 2)
>>> print(qml.draw(lowered_qnode, level=0)())
    0: ─╭●─╭●─┤  <Z>
    1: ─├●─├●─┤
    2: ─╰S─├●─┤
    3: ────╰X─┤

We can replace the multi-controlled iX gate by running the transform:

>>> print(qml.draw(lowered_qnode, level=1)())
    0: ──────────────╭●───────────╭●────┤  <Z>
    1: ──────────────├●───────────├●────┤
    2: ────────╭●────│──────╭●────│─────┤
    3: ──H──T†─╰X──T─╰X──T†─╰X──T─╰X──H─┤
@replace_iX_gate
@qml.qnode(device=dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.Hadamard(wires=1)
    qml.Barrier(wires=[0,1])
    qml.X(0)

    # begin multi-controlled iX gate

    qml.ctrl(qml.S(wires=[2]), control=[0, 1])
    qml.MultiControlledX(wires=[0, 1, 2, 3])

    # end multi-controlled iX gate

    qml.Hadamard(wires=1)
    qml.Barrier(wires=[0,1])
    qml.X(0)
    return qml.expval(qml.Z(0))

The relative multi-controlled iX gate (CS, Toffoli) is then replaced before execution.