qrunch.quantum.circuits.compilers

Module containing compilers that can convert a circuit to a universal gate circuit.

Classes

BaseGate

A gate that is fully expanded and should not be touched further by the compiler.

Compiler

Protocol for compilers that compile to universal gates.

Expanded

Sequence of gates marked as the result of a gate expansion.

GateHandler

Dataclass representing a gate expansion rule.

PerGateCompiler

Compiler that compiles each gate individually.

class BaseGate

Bases: object

A gate that is fully expanded and should not be touched further by the compiler.

All fields are immutable (frozen=True) so an instance can be safely reused.

Parameters:

gate – The gate that is fully expanded.

__init__(gate: Gate) None
Parameters:

gate (Gate)

Return type:

None

gate: Gate
class Compiler

Bases: Protocol

Protocol for compilers that compile to universal gates.

__init__(*args, **kwargs)
compile(circuit: Circuit) UniversalGateCircuit

Compile a circuit consisting of anything into universal gates.

The universal gate set used here is Rx, Ry, Rz, and CX.

Parameters:

circuit (Circuit) – circuit to compile.

Returns:

compiled universal gate circuit.

Return type:

UniversalGateCircuit

class Expanded

Bases: object

Sequence of gates marked as the result of a gate expansion.

The sequence will be further processed in a recursive call to the compiler. All fields are immutable (frozen=True) so an instance can be safely reused.

Parameters:

gates – The gates resulting from the expansion.

__init__(gates: Iterator[Gate]) None
Parameters:

gates (Iterator[Gate])

Return type:

None

gates: Iterator[Gate]
class GateHandler

Bases: object

Dataclass representing a gate expansion rule.

All fields are immutable (frozen=True) so an instance can be safely reused.

Parameters:
  • gate_type – The type of gates to handle.

  • handler – The function that expands a gate.

  • name – The name of the handler, taken from the function name.

__init__(gate_type: type[Gate], handler: Callable[[Gate], Iterator[Gate] | None]) None
Parameters:
  • gate_type (type[Gate])

  • handler (Callable[[Gate], Iterator[Gate] | None])

Return type:

None

gate_type: type[Gate]
handler: Callable[[Gate], Iterator[Gate] | None]
name: str
class PerGateCompiler

Bases: Compiler

Compiler that compiles each gate individually.

__init__(base_gates: tuple[type[~qrunch.quantum.gates.gates_protocols.Gate], ...] = (<class 'qrunch.quantum.gates.universal_gates.RXGate'>, <class 'qrunch.quantum.gates.universal_gates.RYGate'>, <class 'qrunch.quantum.gates.universal_gates.RZGate'>, <class 'qrunch.quantum.gates.universal_gates.CXGate'>, <class 'qrunch.quantum.gates.universal_gates.PGate'>), gate_handlers: list[~qrunch.quantum.circuits.compilers.GateHandler] | None = None, *, keep_annotations: bool = False) None

Initialize a PerGateCompiler.

The compiler consists of a map of gate expansion rules. Each rule is a gate type paired with a function that takes a gate and returns a sequence of gates. Expanded sequences will be repeatedly expanded until we reach a universal gate, where this gate will then be emitted.

The expansion rules can be added using the gate_handlers argument to the compiler constructor or using the push_handler method. To derive a new compiler from an existing one, use the derive method.

Parameters:
  • base_gates (tuple[type[Gate], ...]) – Tuple of the type of gates that should be passed through without expansion.

  • gate_handlers (list[GateHandler] | None) – List of gate expansion rules.

  • keep_annotations (bool) – If True, annotation gates will be kept in the compiled circuit.

Return type:

None

property base_gates: tuple[type[Gate], ...]

Return a copy of the base gates.

compile(circuit: Circuit) UniversalGateCircuit

Compile a circuit consisting of single and double excitation gates into universal gates.

The universal gate set used here is Rx, Ry, Rz, and CX.

Parameters:

circuit (Circuit) – circuit to compile.

Returns:

compiled universal gate circuit.

Return type:

UniversalGateCircuit

derive() PerGateCompiler

Create a new compiler, based on this one.

Create a new PerGateCompiler with the same gate compilers. This can be used to write new compilers that inherit expansion rules from other compilers.

Returns:

New PerGateCompiler instance.

Return type:

PerGateCompiler

property gate_handlers: list[GateHandler]

Return a copy of the gate handlers.

map_gates(gates: Iterable[Gate]) Iterator[Gate]

Transform gates to iterator that recursively expands gates using the handlers.

Any recursion on expanded gate streams should go through this method when an Expanded is encountered, while any BaseGate will be checked emitted if it is a universal gate.

Parameters:

gates (Iterable[Gate]) – Iterator of gates to expand.

Yields:

Expanded gates.

Return type:

Iterator[Gate]

with_handler(gate_type: type[G], fn: Callable[[G], Iterator[Gate] | None], *, allow_overrides: bool = False) PerGateCompiler

Register a handler for a gate type.

The registered handler will be used to expand gates of the given type and all its subclasses. Handlers will be invoked in the reverse order of registration, i.e. the last registered handler will be attempted first. This way, more specific handlers early in the list can override more general handlers later in the list.

If a handler already exists for a more specific type of the gate_type, i.e. gate_type itself or a subclass of gate_type, an error will be raised. This behavior can be overridden by setting allow_overrides to True.

A handler should return either an iterator over expanded gates or None if the gate cannot handle its input after all. In that case, the compiler will attempt to use a later handler to expand the gate.

Parameters:
  • gate_type (type[G]) – Gate type to handle.

  • fn (Callable[[G], Iterator[Gate] | None]) – Function that expands a gate into universal gates for the given platform.

  • allow_overrides (bool) – If True, allow overriding existing handlers for the same gate type.

Returns:

self.

Return type:

PerGateCompiler

Modules

nam_qeb

Module defining the NAM QEB compiler.