qat.comm.datamodel.ttypes.GateDefinition
Example: GateDefinition
of gate H
from qat.lang.AQASM import Program
prog = Program()
circuit = prog.to_circ()
print(circuit.gateDic["H"])
GateDefinition(name='H', arity=1, matrix=Matrix(nRows=2, nCols=2, data=[ComplexNumber(re=0.7071067811865475, im=0.0), ComplexNumber(re=0.7071067811865475, im=0.0), ComplexNumber(re=0.7071067811865475, im=0.0), ComplexNumber(re=-0.7071067811865475, im=0.0)]), is_ctrl=False, is_dag=None, is_trans=None, is_conj=None, subgate=None, syntax=GSyntax(name='H', parameters=[]), nbctrls=None, circuit_implementation=None)
Note
Class GateDefinition
is not designed to be instantiated manually.
Please refer to the Writing quantum circuits section or the
qat.lang
module to create your own circuits
- class qat.comm.datamodel.ttypes.GateDefinition
A gate definition describes the implementation of a quantum gate. A quantum gate can be defined by:
a unitary matrix
a function of another gate (i.e. control of a subgate, dagger of a subgate, …)
a subcircuit
Instance attributes:
matrix (optional): the matrix implementation of the gate. A matrix is defined with the following attributes:
nCols (int): the number of columns in the matrix
nRows (int): the number of rows in the matrix
data (list): list of complex numbers describing the content of this matrix
is_ctrl (bool, optional, deprecated): indicates if the gate is a controled version of another gate
is_dag (bool, optional): indicates if the gate is a dagger version of another gate
is_conj (bool, optional): indicates if the gate is a conjugate version of another gate
is_trans (bool, optional): indicates if the gate is a transpose version of another gate
nbctrls (int, optional): signifies that the gate is a multiple controled version of another gate. If set to a non-zero number, subgate will store the corresponding subgate.
subgate (str, optional): will store the name of the subgate if any one of the .is_ctrl, .is_dag, .is_conj, .is_trans is true, or if .nbctrls is a strict positive integer.
arity (int): an integer representing the number of qubits on which this gate can be applied
syntax (optional): the syntax of the gate (if any). A syntax is defined with the following attributes:
name (str): name of the gate (e.g. “H”, “RZ”, etc.)
parameters (list): parameters used to build the gate
circuit_implementation (optional): if the gate has an implementation in the form of a subroutine, this attribute contains the subcircuit corresponding to the gate. Definitions of gates generated with an
AbstractGate
may have this attribute defined. A circuit implementation is defined by the following attributes:ops (list): list of
Op
ancillas (int): number of ancillas
nbqbits (int): number of qubits used by the subroutine
There are three different ways to define the implementation of a gate:
Using a matrix
The definition of a gate is given by a matrix. The attribute matrix
of GateDefinition
will contain the matrix.
from qat.lang.AQASM import Program, H
# Create a circuit
prog = Program()
qbit = prog.qalloc(1)
H(qbit)
circ = prog.to_circ()
# Extract the definition of the gate H
print(f"The ID of the first gate is {circ.ops[0].gate}")
print(circ.gateDic["H"].matrix)
The ID of the first gate is H
Matrix(nRows=2, nCols=2, data=[ComplexNumber(re=0.7071067811865475, im=0.0), ComplexNumber(re=0.7071067811865475, im=0.0), ComplexNumber(re=0.7071067811865475, im=0.0), ComplexNumber(re=-0.7071067811865475, im=0.0)])
Using a subgate
The definition of a gate is given by a subgate and a transformation. The attribute
subgate
of GateDefinition
will contain
the name of the subgate.
from qat.lang.AQASM import Program, H
# Create a circuit
prog = Program()
qbits = prog.qalloc(2)
H.ctrl()(qbits)
circ = prog.to_circ()
# Extract the definition of the gate C-H
print(f"The ID of the first gate is {circ.ops[0].gate}")
definition = circ.gateDic["_0"]
print(f"This gate '_0' controls {definition.nbctrls} time the gate {definition.subgate}")
The ID of the first gate is _0
This gate '_0' controls 1 time the gate H
Using a circuit implementation
The definition of a gate is given by a circuit implementation. The attribute
circuit_implementation
of GateDefinition
contains the definition of the gate.
from qat.lang.AQASM import Program
from qat.lang.AQASM.qftarith import QFT
# Create a circuit
prog = Program()
qbits = prog.qalloc(2)
QFT(2)(qbits)
circ = prog.to_circ()
# Extract the definition of QFT(2)
print(f"The circuit is composed of {len(circ.ops)} gate")
print("The definition of the gate is given by the subcircuit:")
print(circ.gateDic["_0"].circuit_implementation.ops)
The circuit is composed of 1 gate
The definition of the gate is given by the subcircuit:
[Op(gate='H', qbits=[0], type=0, cbits=None, formula=None, remap=None), Op(gate='_2', qbits=[1, 0], type=0, cbits=None, formula=None, remap=None), Op(gate='H', qbits=[1], type=0, cbits=None, formula=None, remap=None)]