Skip to content

single_qubit_gate

try_match_replace_with_default_gate(gate)

Try replacing a given SingleQubitGate with a default SingleQubitGate. It does that by matching the input SingleQubitGate to a default SingleQubitGate.

Returns:

Type Description
SingleQubitGate

A default SingleQubitGate if this SingleQubitGate is close to it,

SingleQubitGate

or the input SingleQubitGate otherwise.

Source code in opensquirrel/ir/single_qubit_gate.py
def try_match_replace_with_default_gate(gate: SingleQubitGate) -> SingleQubitGate:
    """Try replacing a given SingleQubitGate with a default SingleQubitGate.
        It does that by matching the input SingleQubitGate to a default SingleQubitGate.

    Returns:
            A default SingleQubitGate if this SingleQubitGate is close to it,
            or the input SingleQubitGate otherwise.
    """
    from opensquirrel.default_instructions import (
        default_bsr_with_param_set,
        default_single_qubit_gate_set,
    )
    from opensquirrel.ir.default_gates.single_qubit_gates import Rn

    for gate_name in default_single_qubit_gate_set:
        if gate_name in ("Rn", "U"):
            continue

        arguments: tuple[Any, ...] = (gate.qubit,)
        if gate_name in default_bsr_with_param_set:
            arguments += (Float(gate.bsr.angle),)

        possible_gate = default_single_qubit_gate_set[gate_name](*arguments)  # ty: ignore[invalid-argument-type]
        if possible_gate == gate:
            return possible_gate

    nx, ny, nz = gate.bsr.axis.value
    return Rn(gate.qubit, nx=nx, ny=ny, nz=nz, theta=gate.bsr.angle, phi=gate.bsr.phase)