qat.lang.Program
- class qat.lang.Program(default_gate_set=True)
Class for quantum programs.
- Parameters
default_gate_set (bool, optional) – If set to False, the Program is initialized with an empty gate set. The gate set is updated dynamically when one applies gates to the Program via the apply method. Default to True.
- calloc(size=1)
Allocates a new classical bit register of the given size. Returns the register.
- Parameters
size (int) – The number of classical bits to allocate.
- Returns
A fresh classical bit register of size size
- Return type
- cbreak(formula)
Conditional break. The program execution will stop if and only if the formula given as argument evaluates to 1.
- Parameters
formula (
BoolFormula
) – a boolean formula
- cc_apply(cbit, gate, *args)
Conditional application of a quantum gate. The quantum gate will be applied if and only if the control classical bit given as first argument evaluates to 1.
- comment(message)
Add a comment inside the circuit. The comment will appear in the AQASM export of the circuit.
- Parameters
message (str) – a string containing the comment
- conjugate(gate1, gate2, *args)
Apply a sequence of 3 gates. gate2 will be conjugated by gate1. The two gates are required to have the same arity.
- display(**kwargs)
Transforms the program into a circuit and displays it.
- Parameters
kwargs – key word arguments passed to the display method of the circuit
- export(fname)
Exports the Program in AQASM text format.
- Parameters
fname (str) – A file name
- free_ancillae(args)
Releases a set of ancillae
- get_free_ancillae(size=1, class_name=None)
Returns a collection of free ancillae
- logic(cbit, formula)
Performs a classical logic operation using classical bits and stores the result in a classical bit.
- Parameters
cbit (
Cbit
) – the cbit that will store the resultformula (
BoolFormula
) – A boolean formula.
- measure(qbits, cbits=None)
Applies a measurement operator. All measurements are \(Z\) measurements (i.e in the computational basis). Qbits are simply collapsed and not destroyed (i.e they are still usable afterward).
- Parameters
qbits (list<Qbit>) – The qubits to measure specified as a list/register/array
cbits – (list<Cbit>, optional): The classical bits in which to store the result. Default to None. If defaulted/set to None, the results will be stored in classical bits with matching indices.
- new_var(var_type, var_name)
Constructs a new variable bound to the circuit.
- Parameters
var_type (type) – The type of the variable.
var_name (str) – The name of the variable. Raises a
VariableNameNotAvailable
if a variable with the same name is already bound to the circuit.
- Returns
A variable
- Return type
- qalloc(size=1, class_type=None, **kwargs)
Allocates a new qbit register of the given size. Returns the register.
If no constructor is provided, the register will simply behave as an array of qubits.
The two implemented quantum types are
QInt
andQBoolArray
.from qat.lang.AQASM import Program, QInt, QBoolArray prog = Program() qbits = prog.qalloc(5) print(qbits) qint = prog.qalloc(5, QInt) print(qint) qbools = prog.qalloc(5, QBoolArray) print(qbools)
QReg(q[0]..q[4]) QInt(q[5]..q[9]) QBoolArray(q[10]..q[14])
The main purpose of this wrapping process is to provide a higher level interface for some quantum register, depending on the application. This allows to generate complicated quantum circuits without having to deal with low level administrative constraints.
- Parameters
size (int) – the number of qbits to allocate.
class_type (type) – the quantum type of the register
kwargs – any keyword argument is passed to the constructor of the quantum type
- Returns
A fresh qbit register of size size
- Return type
- reset(qblist, cblist=None)
Resets the value of a set of qbits to \(|0\rangle\). and/or a set of cbits to 0.
- Parameters
qblist (list<Qbit>) – The list of qbits
cblist (list<Cbit>, optional) – The list of cbits. Default to [].
- run(*args, **kwargs)
Casts the program into a circuit and calls its .run method.
All arguments are passed to Circuit’s .run method.
- to_circ(include_matrices=True, include_locks=False, inline=False, do_link=True, comparison_eps=1e-20, box_routines=False, **kwargs)
Return a
Circuit
implementing the program.The circuit extraction consists of three stages:
Circuit generation: a simple circuit is extracted from the Program object. Any call to an abstract gate will be left untouched. All circuit implementation attached to abstract gates definition will be ignored at this stage.
Linking: this simple circuit is then linked against:
the gate set aggregated by the program during its construction
any other gate set/abstract gate/python module passed as argument.
The linking process simply attach to each abstract gate a circuit implementation using the function specified at definition of the abstract gate.
Inlining: these implementations are then inlined in place, turning the circuit into a (potentially) very long sequence of gates/operations. By default this step is skipped, as it is very demanding.
All these steps can be controlled using various parameters detailed below.
- Parameters
include_matrices (bool, optional) – if set to True, matrices will be generated and included in the circuit. Defaults to True.
include_locks (bool) – If set to True, additional gates will be added on the ancilla wire to signify lock and release of the ancilla. Defaults to False. This can be used to debug ancilla usage or further optimize the circuit later on. Lock/release operations can be removed from the circuit via the .remove_locks() method.
do_link (bool) – if set to False, skips the linking step. Default to True.
inline (bool) – if set to False, no inlining/linking will happen. Default to False.
comparison_eps (float) – Optional argument to set the matrix/gate parameters comparison threshold. If two parameters are comparison_eps close, they will be considered as identical, resulting in the indentification of their correspondig gates (thus saving space and time). Default value is 1e-20.
box_routines (bool) – If set to True, routines will be systematically boxed before being included inside the initial circuit (the one generated during the Circuit generation step). Setting this option to True can save up a lot of time for repetitive circuits.
**kwargs – other arguments passed to the linker. See below.
- Keyword Arguments
submatrices_only (bool) –
if set to True, only submatrices will be generated and included in the circuit. Default to False.
Warning
Setting this argument to True will decrease (by a lot) the circuit generation time. For instance, using the default settings, a RZ rotation controlled 10 times will end up generating a matrix of size (\(2^{11} \times 2^{11}\)). By setting this argument to True, only a \(2\times 2\) matrix will be generated. However, not all simulators are able to infer a matrix from the structure of the gate. For now, only
LinAlg
is able to do so. In particular, this means that you should set this option to True to simulate circuits onMPS
.keep (list<str>) – if set to a list of gate names, these gates won’t be inlined by the linker. Default to None.
link (list) – a list of
AbstractGate
,GateSet
, or python packages to pass to the linker.
- Returns
A circuit implementing the program
- Return type