qat.lang.linking.Linker
- class qat.lang.linking.Linker(gate_set=None, include_matrices=True, keep=None, link=None, submatrices_only=False, inline=False, include_locks=False)
The purpose of a linker is to crawl a circuit and attach implementations to gates. These implementations are specified via a
GateSet
object (more or less a collection ofAbstractGate
).The main method of this class is the .link method.
The following example uses a linker to replace CNOT gates by H and CZ gates:
from qat.lang.AQASM import * from qat.core.gate_set import GateSet from qat.lang.linking.linker import Linker prog = Program() qbits = prog.qalloc(2) CNOT(qbits) circuit = prog.to_circ() @build_gate('CNOT', [], arity=2) def my_cnot_implementation(): rout = QRoutine() wires = rout.new_wires(2) with rout.compute(): H(wires[1]) CSIGN(wires) rout.uncompute() return rout # We initialize a Linker with an empty gate set since we want # to override the default CNOT implementation linker = Linker(gate_set=GateSet()) linker.add_signature(my_cnot_implementation) print("Before linking:") for op in circuit.iterate_simple(): print(op) linker.link(circuit) print("After linking:") for op in circuit.iterate_simple(): print(op)
Before linking: ('CNOT', [], [0, 1]) After linking: ('H', [], [1]) ('CSIGN', [], [0, 1]) ('H', [], [1])
- Parameters
gate_set (
GateSet
) – A gate set to start the linking with. Optional. Defaults to the default pyAQASM gate set.include_matrices (bool, optional) – if set to True, matrices will be generated and included in the circuit. Defaults to True.
keep (list<str>) – if set to a list of gate names, these gates will be skipped by the linker. Default to None.
link (list) – a list of
AbstractGate
,GateSet
, or python modules to pass to the linker.submatrices_only (bool) – if set to True, only submatrices will be generated and included in the circuit. For instance a CTRL(Y) will only generate the matrix for Y, thus saving memory. Default to True.
inline (bool, optional) – if set to True, subcircuit implementations will be inlined in the main body of the circuit. This might increase the memory footprint of the circuit and linking cost. Default to False.
include_locks (bool, optional) – if set to False, removes all lock/release operators after the linking process. If set to True, these operators will remain. These operators are here for debug purpose only. Defaults to False.
- add_signature(gate_signature)
Adds a gate signature to the current gate set of the Linker. This gate should not be present in the current gate set.
- Parameters
gate_signature (
GateSignature
) – a gate signature
- clear_gate_set(default_gates=True)
Resets the content of the internal gate set of the Linker.
- Parameters
default_gates (bool) – If set to True, the default gate set of pyAQASM will be included in the fresh gate set. Optional. Defaults to True.
- compile(batch, _specs)
Applies the linker to all the jobs in a batch.
- link(circuit)
Uses the internal gate set of the Linker to link gate implementations.
Warning
The linking happens in place (thus modifies the input circuit).
- Parameters
circuit (
qat.core.Circuit
) – a quantum circuit
- link_gates(circuit)
Uses the internal gate set of the Linker to instantiate the gates of a circuit.
Warning
The linking happens in place (thus modifies the input circuit).
- Parameters
circuit (
qat.core.Circuit
) – a quantum circuit
- link_matrices(circuit)
Link the matrix implementations to the gate definitions of a circuit.
Warning
The linking happens in place (thus modifies the input circuit).
- Parameters
circuit (
qat.core.Circuit
) – a quantum circuit