Circuit

Submodules

circuit module

class tequila_code.circuit.circuit.Moment(gates: List[QGateImpl] = None, sort=False)[source]

Bases: QCircuit

the class which represents a set of simultaneously applicable gates.

with_gate:

attempt to add a gate to the moment, replacing any gate it may conflict with.

with_gates:

attempt to add multiple gates to the moment, replacing any gates they may conflict with.

add_gate(gate: QCircuit | QGateImpl)[source]

add a gate to the moment.

Parameters:

gate – the desired gate.

Returns:

a new moment, of self + a new gate

Return type:

Moment

as_circuit()[source]

convert back into the unrestricted QCircuit. :returns: a circuit with the same gates as self. :rtype: QCircuit

property canonical_depth

gate depth of the abstract circuit in alternating layer form. :returns: int :rtype: depth of the alternating layer form.

property canonical_moments

Break self up into parametrized and unparametrized layers. :returns: list of 2 Moments, one of which may be empty. :rtype: list

property depth

gate depth of the abstract circuit. :returns: int :rtype: the depth.

static from_moments(moments: List)[source]
Raises:

TequilaException

fully_parametrized()[source]

Todo: Why not just inherit from base? :returns: whether or not EVERY gate in self.gates is parameterized. :rtype: bool

property moments

Divide self into subcircuits representing layers of simultaneous gates. Attempts to minimize gate depth. :returns: list of Moment objects. :rtype: list

replace_gate(position, gates)[source]

substitute a gate at a given position with other gates. :param position: where in the list of gates the gate to be replaced occurs. :param gates: the gates to replace the unwanted gate with.

Returns:

self, with unwanted gate removed and new gates inserted. May not be a moment.

Return type:

QCircuit or Moment

with_gate(gate: QCircuit | QGateImpl)[source]

Add a gate, or else replace some gate with it.

Parameters:

gate – the gate to insert into the moment.

Returns:

a New moment with the proper gates.

Return type:

Moment

with_gates(gates)[source]

with gate, but on multiple gates.

Parameters:

gates – list of QGates

Returns:

a new Moment, with the desired gates insert into self.

Return type:

Moment

static wrap_gate(gate: QGateImpl)[source]
Parameters:

gate (QGateImpl:) – the gate, to wrap as a moment

Returns:

a moment with one gate in it.

Return type:

Moment

class tequila_code.circuit.circuit.QCircuit(gates=None, parameter_map=None)[source]

Bases: object

Fundamental class representing an abstract circuit.

canonical_depth

the depth of the circuit, if converted to alternating parametrized and unparametrized layers.

canonical_moments

returns the circuit as a list of Moment objects alternating between parametrized and unparametrized layers.

depth

returns the gate depth of the circuit.

gates

returns the gates in the circuit, as a list.

moments

returns the circuit as a list of Moment objects.

n_qubits

the number of qubits on which the circuit operates.

numbering

returns the numbering convention use by tequila circuits.

qubits

returns a list of qubits acted upon by the circuit.

make_parameter_map:
add_controls(control, inpl: bool | None = False) QCircuit | None[source]
Depending on the truth value of inpl:
  • return controlled version of self with control as the control qubits if inpl;

  • mutate self so that the qubits in control are added as the control qubits if not inpl.

Raise ValueError if there any qubits in common between self and control.

property canonical_depth

gate depth of the abstract circuit in alternating layer form. :returns: int :rtype: depth of the alternating layer form.

property canonical_moments

Divide self into subcircuits of alternating unparametrized and parametrized layers. :rtype: list of Moment objects.

dagger()[source]
Returns:

The adjoint of the circuit

Return type:

QCircuit

property depth

gate depth of the abstract circuit. :returns: int :rtype: the depth.

export_to(*args, **kwargs)[source]

Export to png, pdf, qpic, tex with qpic backend Convenience: see src/tequila/circuit/qpic.py - export_to for more Parameters

extract_variables() list[source]

return a list containing all the variable objects contained in any of the gates within the unitary including those nested within gates themselves.

Returns:

the variables of the circuit

Return type:

list

static from_moments(moments: List)[source]

build a circuit from Moment objects.

Parameters:

moments (list:) – a list of Moment objects.

property gates
insert_gates(positions, gates)[source]

See replace_gates

is_fully_parametrized()[source]
Returns:

whether or not all gates in the circuit are paremtrized

Return type:

bool

is_fully_unparametrized()[source]
Returns:

whether or not all gates in the circuit are unparametrized

Return type:

bool

is_mixed()[source]
is_primitive()[source]

Check if this is a single gate wrapped in this structure :return: True if the circuit is just a single gate

make_parameter_map() dict[source]
Returns:

  • ParameterMap of the circuit (A dictionary with)

  • keys (variables in the circuit)

  • values (list of all gates and their positions in the circuit)

  • e.g. result[Variable(“a”)] = [(3, Rx), (5, Ry), …]

map_qubits(qubit_map)[source]

E.G. Rx(1)Ry(2) –> Rx(3)Ry(1) with qubit_map = {1:3, 2:1}

Parameters:

qubit_map – a dictionary which maps old to new qubits

Return type:

A new circuit with mapped qubits

map_variables(variables: dict, *args, **kwargs)[source]
Parameters:

variables – dictionary with old variable names as keys and new variable names or values as values

Return type:

Circuit with changed variables

max_qubit()[source]

Returns: int:

Highest index of qubits in the circuit

property moments

Divide self into subcircuits representing layers of simultaneous gates. Attempts to minimize gate depth. :returns: list of Moment objects. :rtype: list

property n_qubits
property numbering: BitNumbering
property qubits
replace_gates(positions: list, circuits: list, replace: list = None)[source]

Notes

Replace or insert gates at specific positions into the circuit at different positions (faster than multiple calls to replace_gate)

Parameters:
  • positions (list of int:) – the positions at which the gates should be added. Always refer to the positions in the original circuit

  • circuits (list or QCircuit:) – the gates to add at the corresponding positions

  • replace (list of bool: (Default value: None)) – Default is None which corresponds to all true decide if gates shall be replaces or if the new parts shall be inserted without replacement if replace[i] = true: gate at position [i] will be replaces by gates[i] if replace[i] = false: gates[i] circuit will be inserted at position [i] (beaming before gate previously at position [i])

Return type:

new circuit with inserted gates

sort_gates()[source]

sort self into subcircuits corresponding to all simultaneous operations, greedily; then reinitialize gates. :rtype: None

to_networkx()[source]

Turn a given quantum circuit from tequila into graph form via NetworkX :param self: tq.gates.QCircuit :return: G, a graph in NetworkX with qubits as nodes and gate connections as edges

verify() bool[source]

make sure self is built properly and of the correct types. :returns: whether or not the circuit is properly constructed. :rtype: bool

static wrap_gate(gate: QGateImpl)[source]

take a gate and return a qcircuit containing only that gate. :param gate: the gate to wrap in a circuit. :type gate: QGateImpl

Returns:

a one gate circuit.

Return type:

QCircuit

tequila_code.circuit.circuit.find_unused_qubit(U0: QCircuit = None, U1: QCircuit = None) int[source]

Function that checks which are the active qubits of two circuits and provides an unused qubit that is not among them. If all qubits are used it adds a new one.

Parameters:
  • U0 (QCircuit, corresponding to the first state.)

  • U1 (QCircuit, corresponding to the second state.)

Returns:

control_qubit

Return type:

int

compiler module

class tequila_code.circuit.compiler.CircuitCompiler(multitarget=False, multicontrol=False, trotterized=False, generalized_rotation=False, exponential_pauli=False, controlled_exponential_pauli=False, hadamard_power=False, controlled_power=False, power=False, toffoli=False, controlled_phase=False, phase=False, phase_to_z=False, controlled_rotation=False, swap=False, cc_max=False, gradient_mode=False, ry_gate=False, y_gate=False, ch_gate=False, hadamard=False)[source]

Bases: object

an object that performs abstract compilation of QCircuits and Objectives.

Note

see init for attributes, since all are specified there

compile_objective()[source]

perform compilation on an entire objective

compile_objective_argument()[source]

perform compilation on a single arg of objective

compile_circuit:

perform compilation on a circuit.

classmethod all_flags_true(*args, **kwargs)[source]
compile_circuit(abstract_circuit: QCircuit, variables=None, *args, **kwargs) QCircuit[source]

compile a circuit. :param abstract_circuit: the circuit to compile. :type abstract_circuit: QCircuit :param variables: (Default value = None):

list of the variables whose gates, specifically, must compile. Used to prevent excess compilation in gates whose parameters are fixed. Default: compile every single gate.

Parameters:
  • args

  • kwargs

Return type:

QCircuit; a compiled circuit.

compile_objective(objective, *args, **kwargs)[source]

Compile an objective.

Parameters:
  • objective (Objective:) – the objective.

  • args

  • kwargs

Return type:

the objective, compiled

compile_objective_argument(arg, *args, **kwargs)[source]

Compile an argument of an objective.

Parameters:
  • arg – the term to compile

  • args

  • kwargs

Return type:

the arg, compiled

classmethod standard_gate_set(*args, **kwargs)[source]
exception tequila_code.circuit.compiler.TequilaCompilerException(msg)[source]

Bases: TequilaException

tequila_code.circuit.compiler.change_basis(target, axis=None, name=None, daggered=False)[source]

helper function; returns circuit that performs change of basis. :param target: the qubit having its basis changed :param axis: The axis of rotation to shift into. :param daggered: adjusts the sign of the gate if axis = 1, I.E, change of basis about Y axis. :type daggered: bool:

Return type:

QCircuit that performs change of basis on target qubit onto desired axis

tequila_code.circuit.compiler.compile_ch(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_controlled_phase(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_controlled_power(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_controlled_rotation(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_exponential_pauli_gate(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_generalized_rotation_gate(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_multitarget(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_phase(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_phase_to_z(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_power_base(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_power_gate(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_ry(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_swap(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_to_single_control(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_toffoli(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_trotterized_gate(gate, **kwargs)[source]
tequila_code.circuit.compiler.compile_y(gate, **kwargs)[source]
tequila_code.circuit.compiler.compiler(f)[source]

Decorator for compile functions.

Make them applicable for single gates as well as for whole circuits Note that all arguments need to be passed as keyword arguments

tequila_code.circuit.compiler.do_compile_trotterized_gate(generator, steps, factor, randomize, control)[source]

gates module

tequila_code.circuit.gates.CNOT(control: int, target: int) QCircuit[source]

Convenience CNOT initialization

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

Return type:

QCircuit object

tequila_code.circuit.gates.CRx(control: int, target: int, angle: float) QCircuit[source]

Convenience initialization CRx (controlled Pauli X Rotation)

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

Return type:

QCircuit object

tequila_code.circuit.gates.CRy(control: int, target: int, angle: float) QCircuit[source]

Convenience initialization CRy (controlled Pauli Y Rotation)

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

Return type:

QCircuit object

tequila_code.circuit.gates.CRz(control: int, target: int, angle: float) QCircuit[source]

Convenience initialization CRz (controlled Pauli Z Rotation)

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

Return type:

QCircuit object

tequila_code.circuit.gates.CX(control: int, target: int) QCircuit[source]

Convenience initialization CX (CNOT)

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

Return type:

QCircuit object

tequila_code.circuit.gates.CY(control: int, target: int) QCircuit[source]

Convenience initialization CY (controlled Pauli Y)

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

Return type:

QCircuit object

tequila_code.circuit.gates.CZ(control: int, target: int) QCircuit[source]

Convenience initialization CZ (controlled Pauli Z)

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

Return type:

QCircuit object

tequila_code.circuit.gates.ExpPauli(paulistring: PauliString | str | dict, angle, control: list | int = None, *args, **kwargs)[source]

Exponentiated Pauligate:

ExpPauli(PauliString, angle) = exp(-i* angle/2* PauliString)

Parameters:
  • paulistring (Union[PauliString :) – given as PauliString structure or as string or dict or list if given as string: Format should be like X(0)Y(3)Z(2) if given as list: Format should be like [(0,’X’),(3,’Y’),(2,’Z’)] if given as dict: Format should be like { 0:’X’, 3:’Y’, 2:’Z’ }

  • angle – the angle (will be multiplied by paulistring coefficient if there is one)

  • control (Union[list :) – control qubits

  • paulistring

  • str]

  • control

  • int] – (Default value = None)

Returns:

Gate wrapped in circuit

Return type:

type

tequila_code.circuit.gates.GenRot(*args, **kwargs)[source]
tequila_code.circuit.gates.GeneralizedRotation(angle: List[Hashable] | List[Real], generator: QubitHamiltonian, control: list | int = None, eigenvalues_magnitude: float = 0.5, p0=None, steps: int = 1, assume_real=False) QCircuit[source]

Notes

A gates which is shift-rule differentiable
  • its generator only has two distinguishable eigenvalues

  • it is then differentiable by the shift rule

  • eigenvalues_magnitude needs to be given upon initialization (this is “r” from Schuld et. al. and the default is r=1/2)

  • the generator will not (!) be verified to fullfill the properties

Compiling will be done in analogy to a trotterized gate with steps=1 as default

The gate will act in the same way as rotations and exppauli gates

\[U_{G}(\text{angle}) = e^{-i\frac{\text{angle}}{2} G}\]
Parameters:
  • angle – numeric type or hashable symbol or tequila objective

  • generator – tequila QubitHamiltonian or any other structure with paulistrings

  • control – list of control qubits

  • eigenvalues_magnitude – magnitude of eigenvalues, in most papers referred to as “r” (default 0.5)

  • p0 – possible nullspace projector (if the rotation is happens in Q = 1-P0). See arxiv:2011.05938

  • steps – possible Trotterization steps (default 1)

Return type:

The gate wrapped in a circuit

tequila_code.circuit.gates.Givens(first: int, second: int, control: int | list = None, angle: float = None, *args, **kwargs) QCircuit[source]

Notes

Givens gate G .. math:

G = e^{-i\theta \frac{(Y \otimes X - X \otimes Y )}{2}}
Parameters:
  • first (int) – target qubit

  • second (int) – target qubit

  • control – int or list of ints

  • angle – numeric type (fixed exponent) or hashable type (parametrized exponent), theta in the above formula

Return type:

QCircuit

tequila_code.circuit.gates.H(target: list | int, control: list | int = None, power=None, angle=None, *args, **kwargs) QCircuit[source]

Notes

Hadamard gate

Parameters:
  • target – int or list of int

  • control – int or list of int

  • power

    numeric type (fixed exponent) or hashable type (parametrized exponent) angle similar to power, but will be interpreted as .. math:

    U(\text{angle})=e^{-i\frac{angle}{2} generator}
    

    the default is angle=pi .. math:

    U(\pi) = H
    

    If angle and power are given both, tequila will combine them

Return type:

QCircuit object

tequila_code.circuit.gates.PauliGate(paulistring: PauliString | str | dict, control: list | int = None, *args, **kwargs) QCircuit[source]

Functions that converts a Pauli string into the corresponding quantum circuit.

Parameters:
  • paulistring (Union[PauliString , str, dict])

  • list (if given as)

  • string (if given as)

  • list

  • dict (if given as)

  • control (Union[list, int] : (Default value = None)) – control qubits

Raises:

Exception – Not a Pauli Operator.:

Returns:

U

Return type:

QCircuit object corresponding to the Pauli string.

tequila_code.circuit.gates.Phase(target: list | int, control: list | int = None, angle: Hashable | Number = None, *args, **kwargs) QCircuit[source]

Notes

Initialize an abstract phase gate which acts as

\[\begin{split}S(\phi) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\phi} \end{pmatrix}\end{split}\]
Parameters:
  • angle – defines the phase, can be numeric type (static gate) or hashable non-numeric type (parametrized gate)

  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

tequila_code.circuit.gates.PowerGate(name: str, target: list | int, power: float = None, control: list | int = None, generator: QubitHamiltonian = None, *args, **kwargs)[source]

Initialize a (potentially parametrized) gate which is supported on the backend

Parameters:
  • name (str) – name of the gate on the backend (usually, H, X, Y, Z)

  • target – int or list of int

  • power – numeric type (fixed exponent) or hashable type (parametrized exponent) will be interpreted as

  • angle

    similar to power, but will be interpreted as .. math:

    U=e^{-i\frac{angle}{2} generator}
    

  • control – int or list of int

tequila_code.circuit.gates.QGate(name, target: list | int, control: list | int = None, generator: QubitHamiltonian = None)[source]
tequila_code.circuit.gates.QubitExcitation(angle: Real | Variable | Hashable, target: List, control=None, assume_real: bool = False, compile_options='optimize')[source]

A Qubit Excitation, as described under “qubit perspective” in https://doi.org/10.1039/D0SC06627C For the Fermionic operators under corresponding Qubit encodings: Use the chemistry interface :param angle: the angle of the excitation unitary :param target: even number of qubit indices interpreted as [0,1,2,3….] = [(0,1), (2,3), …]

i.e. as qubit excitations from 0 to 1, 2 to 3, etc

Parameters:
  • control – possible control qubits

  • assume_real – assume the wavefunction on which this acts is always real (cheaper gradients: see https://doi.org/10.1039/D0SC06627C)

Return type:

QubitExcitation gate wrapped into a tequila circuit

class tequila_code.circuit.gates.QubitExcitationImpl(angle, target, generator=None, p0=None, assume_real=True, control=None, compile_options=None)[source]

Bases: GeneralizedRotationImpl

compile(exponential_pauli=False, *args, **kwargs)[source]
map_qubits(qubit_map: dict)[source]
tequila_code.circuit.gates.RotationGate(axis: int, angle: Hashable | Number, target: list | int, control: list | int = None, assume_real=False)[source]

Notes

Initialize an abstract rotation gate of the form

\[R_{\text{axis}}(\text{angle}) = e^{-i\frac{\text{angle}}{2} \sigma_{\text{axis}}}\]
Parameters:
  • axis – integer 1 for x, 2 for y, 3 for z

  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

  • target – integer or list of integers

  • control – integer or list of integers

  • assume_real – enable improved gradient compilation for controlled gates (wavefunction needs to be real)

Return type:

QCircuit object with this RotationGate

tequila_code.circuit.gates.Rp(paulistring: PauliString | str, angle, control: list | int = None, *args, **kwargs)[source]

Same as ExpPauli

tequila_code.circuit.gates.Rx(angle, target: list | int, control: list | int = None, assume_real=False) QCircuit[source]

Notes

Rx gate of the form

\[R_{x}(\text{angle}) = e^{-i\frac{\text{angle}}{2} \sigma_{x}}\]
Parameters:
  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

  • target – integer or list of integers

  • control – integer or list of integers

  • assume_real – enable improved gradient compilation for controlled gates

Return type:

QCircuit object with this RotationGate

tequila_code.circuit.gates.Ry(angle, target: list | int, control: list | int = None, assume_real=False) QCircuit[source]

Notes

Ry gate of the form

\[R_{y}(\text{angle}) = e^{-i\frac{\text{angle}}{2} \sigma_{y}}\]
Parameters:
  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

  • target – integer or list of integers

  • control – integer or list of integers

Return type:

QCircuit object with this RotationGate

tequila_code.circuit.gates.Rz(angle, target: list | int, control: list | int = None, assume_real=False) QCircuit[source]

Notes

Rz gate of the form

\[R_{z}(\text{angle}) = e^{-i\frac{\text{angle}}{2} \sigma_{z}}\]
Parameters:
  • angle – Hashable type (will be treated as Variable) or Numeric type (static angle)

  • target – integer or list of integers

  • control – integer or list of integers

  • Returns

  • RotationGate (QCircuit object with this)

  • -------

tequila_code.circuit.gates.S(target: list | int, control: list | int = None) QCircuit[source]

Notes

\[\begin{split}S = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\frac{\pi}{2}} \end{pmatrix}\end{split}\]
Parameters:
  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

tequila_code.circuit.gates.SWAP(first: int, second: int, angle: float = None, control: int | list = None, power: float = None, *args, **kwargs) QCircuit[source]

Notes

SWAP gate, order of targets does not matter

Parameters:
  • first (int) – target qubit

  • second (int) – target qubit

  • angle (numeric type or hashable type) – exponent in the for e^{-i a/2 G}

  • control – int or list of ints

  • power – numeric type (fixed exponent) or hashable type (parametrized exponent in the form (SWAP)^n

Return type:

QCircuit

tequila_code.circuit.gates.T(target: list | int, control: list | int = None)[source]

Notes

Fixed phase gate

\[\begin{split}T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\frac{\pi}{4}} \end{pmatrix}\end{split}\]
Parameters:
  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

tequila_code.circuit.gates.Toffoli(first: int, second: int, target: int) QCircuit[source]

Convenience Toffoli initialization

Parameters:
  • first (int) – first control qubit

  • second (int) – second control qubit

  • target (int) – target qubit

Return type:

QCircuit object

tequila_code.circuit.gates.Trotterized(generator: QubitHamiltonian = None, steps: int = 1, angle: Hashable | Real | Variable = None, control: list | int = None, randomize=False, *args, **kwargs) QCircuit[source]
generator :

generator of the gate U = e^{-i

rac{angle}{2} G }
angles :

coefficients for each generator

steps :

trotter steps

control :

control qubits

generators: QubitHamiltonian :

The generator of the gate

steps: int :

Trotter Steps

angle: typing.Hashable :

A symbol that will be converted to a tq.Variable

numbers.Real :

A fixed real number

Variable :

A tequila Variable

control: control qubits Returns ——- QCircuit

tequila_code.circuit.gates.U(theta, phi, lambd, target: list | int, control: list | int = None) QCircuit[source]

Notes

Convenient gate, one of the abstract gates defined by OpenQASM.

\[ \begin{align}\begin{aligned}\begin{split}U(\theta, \phi, \lambda) = R_z(\phi)R_x(-\pi/2)R_z(\theta)R_x(\pi/2)R_z(\lambda) U(\theta, \phi, \lambda) = \begin{pmatrix} e^{-i \frac{\phi}{2}} & 0 \\ 0 & e^{i \frac{\phi}{2}} \end{pmatrix} \begin{pmatrix} \cos{-\frac{\pi}{4}} & -i \sin{-\frac{\pi}{4}} \\ -i \sin{-\frac{\pi}{4}} & \cos{-\frac{\pi}{4}} \end{pmatrix} \begin{pmatrix} e^{-i \frac{\theta}{2}} & 0 \\ 0 & e^{i \frac{\theta}{2}} \end{pmatrix} \begin{pmatrix} \cos{\frac{\pi}{4}} & -i \sin{\frac{\pi}{4}} \\ -i \sin{\frac{\pi}{4}} & \cos{\frac{\pi}{4}} \end{pmatrix} \begin{pmatrix} e^{-i \frac{\lambda}{2}} & 0 \\ 0 & e^{i \frac{\lambda}{2}} \end{pmatrix}\end{split}\\\begin{split}U(\theta, \phi, \lambda) = \begin{pmatrix} \cos{\frac{\theta}{2}} & -e^{i \lambda} \sin{\frac{\theta}{2}} \\ e^{i \phi} \sin{\frac{\theta}{2}} & e^{i (\phi+\lambda)} \cos{\frac{\theta}{2}} \end{pmatrix}\end{split}\end{aligned}\end{align} \]
Parameters:
  • theta – first parameter angle

  • phi – second parameter angle

  • lamnd – third parameter angle

  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

tequila_code.circuit.gates.X(target: list | int, control: list | int = None, power=None, angle=None, *args, **kwargs) QCircuit[source]

Notes

Pauli X Gate

Parameters:
  • target – int or list of int

  • control – int or list of int

  • power – numeric type (fixed exponent) or hashable type (parametrized exponent)

  • angle

    similar to power, but will be interpreted as .. math:

    U(\text{angle})=e^{-i\frac{angle}{2} (1-X)}
    

    the default is angle=pi .. math:

    U(\pi) = X
    

    If angle and power are given both, tequila will combine them

Return type:

QCircuit object

tequila_code.circuit.gates.Y(target: list | int, control: list | int = None, power=None, angle=None, *args, **kwargs) QCircuit[source]

Notes

Pauli Y Gate

Parameters:
  • target – int or list of int

  • control – int or list of int

  • power – numeric type (fixed exponent) or hashable type (parametrized exponent)

  • angle

    similar to power, but will be interpreted as .. math:

    U(\text{angle})=e^{-i\frac{angle}{2} (1-Y)}
    

    the default is angle=pi .. math:

    U(\pi) = Y
    

    If angle and power are given both, tequila will combine them

Return type:

QCircuit object

tequila_code.circuit.gates.Z(target: list | int, control: list | int = None, power=None, angle=None, *args, **kwargs) QCircuit[source]

Notes

Pauli Z Gate

Parameters:
  • target – int or list of int

  • control – int or list of int

  • power – numeric type (fixed exponent) or hashable type (parametrized exponent)

  • angle

    similar to power, but will be interpreted as .. math:

    U(\text{angle})=e^{-i\frac{angle}{2} (1-Z)}
    

    the default is angle=pi .. math:

    U(\pi) = Z
    

    If angle and power are given both, tequila will combine them

Return type:

QCircuit object

tequila_code.circuit.gates.iSWAP(first: int, second: int, control: int | list = None, power: float = 1.0, *args, **kwargs) QCircuit[source]

Notes

iSWAP gate .. math:

iSWAP = e^{i\frac{\pi}{4} (X \otimes X + Y \otimes Y )}
Parameters:
  • first (int) – target qubit

  • second (int) – target qubit

  • control – int or list of ints

  • power – numeric type (fixed exponent) or hashable type (parametrized exponent)

Return type:

QCircuit

tequila_code.circuit.gates.u1(lambd, target: list | int, control: list | int = None) QCircuit[source]

Notes

Convenient gate, one of the abstract gates defined by Quantum Experience Standard Header. Changes the phase of a carrier without applying any pulses.

\[\begin{split}from OpenQASM 2.0 specification: u1(\lambda) \sim U(0, 0, \lambda) = R_z(\lambda) = e^{-i\frac{\lambda}{2} \sigma_{z}} also is equal to: u1(\lambda) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\lambda} \end{pmatrix} which is the Tequila Phase gate: u1(\lambda) = Phase(\lambda)\end{split}\]
Parameters:
  • lambd – parameter angle

  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

tequila_code.circuit.gates.u2(phi, lambd, target: list | int, control: list | int = None) QCircuit[source]

Notes

Convenient gate, one of the abstract gates defined by Quantum Experience Standard Header. Uses a single pi/2-pulse.

\[ \begin{align}\begin{aligned}u2(\phi, \lambda) = U(\pi/2, \phi, \lambda) = R_z(\phi + \pi/2)R_x(\pi/2)R_z(\lambda - \pi/2)\\\begin{split}u2(\phi, \lambda) = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\phi+\lambda)} \end{pmatrix}\end{split}\end{aligned}\end{align} \]
Parameters:
  • phi – first parameter angle

  • lambd – second parameter angle

  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

tequila_code.circuit.gates.u3(theta, phi, lambd, target: list | int, control: list | int = None) QCircuit[source]

Notes

Convenient gate, one of the abstract gates defined by Quantum Experience Standard Header The most general single-qubit gate. Uses a pair of pi/2-pulses.

\[\begin{split}u3(\theta, \phi, \lambda) = U(\theta, \phi, \lambda) = \begin{pmatrix} \cos{\frac{\5theta}{2}} & -e^{i \lambda} \sin{\frac{\theta}{2}} \\ e^{i \phi} \sin{\frac{\theta}{2}} & e^{i (\phi+\lambda)} \cos{\frac{\theta}{2}} \end{pmatrix}\end{split}\]
Parameters:
  • theta – first parameter angle

  • phi – second parameter angle

  • lambd – third parameter angle

  • target – int or list of int

  • control – int or list of int

Return type:

QCircuit object

gradient module

tequila_code.circuit.gradient.grad(objective: Objective | QTensor, variable: Variable = None, no_compile=False, *args, **kwargs)[source]

wrapper function for getting the gradients of Objectives,ExpectationValues, Unitaries (including single gates), and Transforms. :param obj (QCircuit,ParametrizedGateImpl,Objective,ExpectationValue,Transform,Variable): structure to be differentiated :param variables (list of Variable): parameter with respect to which obj should be differentiated.

default None: total gradient.

return: dictionary of Objectives, if called on gate, circuit, exp.value, or objective; if Variable or Transform, returns number.

noise module

tequila_code.circuit.noise.AmplitudeDamp(p: float, level: int)[source]

Returns a NoiseModel one QuantumNoise, corresponding to amplitude damping. this channel takes 1 to 0, but leaves 0 unaffected. kraus maps:

E_0= [[1,0],

[0,sqrt(1-p)]]

E_1= [[0,sqrt(p)],

[0,0]]

Parameters:
  • p (float:) – the probability with which the noise is applied.

  • level (int:) – the # of qubits in operations to apply this noise to.

Return type:

NoiseModel

tequila_code.circuit.noise.BitFlip(p: float, level: int)[source]

Returns a NoiseModel with one QuantumNoise, having a kraus map corresponding to applying pauli X with likelihood p.

Parameters:
  • p (float:) – the probability with which the noise is applied.

  • level (int:) – the # of qubits in operations to apply this noise to.

Return type:

NoiseModel

tequila_code.circuit.noise.DepolarizingError(p: float, level: int)[source]

Returns a NoiseModel with one QuantumNoise, having a kraus map corresponding to equal probabilities of each of the three pauli matrices being applied.

Parameters:
  • p (float:) – the probability with which the noise is applied.

  • level (int:) – the # of qubits in operations to apply this noise to.

Return type:

NoiseModel

class tequila_code.circuit.noise.NoiseModel(noises: List[dict | QuantumNoise] = None)[source]

Bases: object

class representing noises to apply to a quantum circuit during simulation.

noises

a list of all the noises to apply.

without_noise_on_level:

remove all noise affecting operations with <level> qubits.

without_noise_op:

remove all noise of a given type, I.E get rid of all bit flips.

without_noise_on_level(level)[source]
without_noise_op(name)[source]
static wrap_noise(other)[source]
tequila_code.circuit.noise.PhaseAmplitudeDamp(p1: float, p2: float, level: int)[source]

Returns a NoiseModel with one QuantumNoise, having a kraus map corresponding to phase and amplitude damping.

Parameters:
  • p1 (float:) – the probability with which phase is damped

  • p2 (float:) – the probability with which amplitude is damped

  • level (int:) – the # of qubits in operations to apply this noise to.

Return type:

NoiseModel

tequila_code.circuit.noise.PhaseDamp(p: float, level: int)[source]

Returns a NoiseModel of one QuantumNoise, having a kraus map corresponding to phase damping; Krauss map is defined following Nielsen and Chuang; E_0= [[1,0],

[0,sqrt(1-p)]]

E_1= [[0,0],

[0,sqrt(p)]]

Parameters:
  • p (float:) – the probability with which the noise is applied.

  • level (int:) – the # of qubits in operations to apply this noise to.

Return type:

NoiseModel

tequila_code.circuit.noise.PhaseFlip(p: float, level: int)[source]

Returns a NoiseModel of one QuantumNoise, having a kraus map corresponding to applying pauli Z with likelihood p.

Parameters:
  • p (float:) – the probability with which the noise is applied.

  • level (int:) – the # of qubits in operations to apply this noise to.

Return type:

NoiseModel

class tequila_code.circuit.noise.QuantumNoise(name: str, probs: List[float], level: int)[source]

Bases: object

class representing a specific quantum noise operation on gates of a certain number of qubits.

name

what noise to apply

probs

the probabilities with which to apply the noise

level

the number of qubits in the gates this noise acts upon

from_dict:

initialize from a dictionary.

static from_dict(d)[source]
property level
property name
prob_length = {'amplitude damp': 1, 'bit flip': 1, 'depolarizing': 1, 'phase damp': 1, 'phase flip': 1, 'phase-amplitude damp': 2}

pyzx module

Add to tequila the ability to make ZX-Calculus

Using the pyzx library: https://github.com/Quantomatic/pyzx

tequila_code.circuit.pyzx.convert_from_pyzx(circuit) QCircuit[source]

Allow convert from pyzx circuit to Tequila circuit

Parameters:

circuit – in pyzx format (pyzx.circuit.Circuit) to be exported to Tequila circuit

Returns:

Tequila circuit

Return type:

QCircuit

tequila_code.circuit.pyzx.convert_to_pyzx(circuit: QCircuit, variables=None)[source]

Allow convert from Tequila circuit to pyzx circuit

Parameters:
  • circuit – in Tequila format to be exported to pyzx

  • variables – optional dictionary with values for variables

Returns:

pyzx circuit

Return type:

pyzx.circuit.Circuit

qasm module

Export QCircuits as qasm code

OPENQASM version 2.0 specification from: A. W. Cross, L. S. Bishop, J. A. Smolin, and J. M. Gambetta, e-print arXiv:1707.03429v2 [quant-ph] (2017). https://arxiv.org/pdf/1707.03429v2.pdf

tequila_code.circuit.qasm.apply_custom_gate(custom_circuit: QCircuit, qregisters_values: list) QCircuit[source]
tequila_code.circuit.qasm.convert_to_open_qasm_2(circuit: QCircuit, variables=None, zx_calculus: bool = False) str[source]

Allow export to OpenQASM version 2.0

Parameters:
  • circuit – to be exported to OpenQASM

  • variables – optional dictionary with values for variables

  • zx_calculus – indicate if y-gates must be transformed to xz equivalents

Returns:

OpenQASM string

Return type:

str

tequila_code.circuit.qasm.export_open_qasm(circuit: QCircuit, variables=None, version: str = '2.0', filename: str = None, zx_calculus: bool = False) str[source]

Allow export to different versions of OpenQASM

Parameters:
  • circuit – to be exported to OpenQASM

  • variables – optional dictionary with values for variables

  • version – of the OpenQASM specification, optional

  • filename – optional file name to save the generated OpenQASM code

  • zx_calculus – indicate if y-gates must be transformed to xz equivalents

Returns:

OpenQASM string

Return type:

str

tequila_code.circuit.qasm.get_angle(name: str) list[source]
tequila_code.circuit.qasm.get_qregister(qreg: str, qregisters: Dict[str, int]) list | int[source]
tequila_code.circuit.qasm.import_open_qasm(qasm_code: str, version: str = '2.0', rigorous: bool = True) QCircuit[source]

Allow import from different versions of OpenQASM

Parameters:
  • qasm_code – string with the OpenQASM code

  • version – of the OpenQASM specification, optional

  • rigorous – indicates whether the QASM code should be read rigorously

Returns:

equivalent to the OpenQASM code received

Return type:

QCircuit

tequila_code.circuit.qasm.import_open_qasm_from_file(filename: str, version: str = '2.0', rigorous: bool = True) QCircuit[source]

Allow import from different versions of OpenQASM from a file

Parameters:
  • filename – string with the file name with the OpenQASM code

  • variables – optional dictionary with values for variables

  • version – of the OpenQASM specification, optional

  • rigorous – indicates whether the QASM code should be read rigorously

Returns:

equivalent to the OpenQASM code received

Return type:

QCircuit

tequila_code.circuit.qasm.name_and_params(g, variables)[source]

Determines the quantum gate name and its parameters if applicable

Parameters:
  • g – gate to get its name

  • variables – dictionary with values for variables

Returns:

name (and parameter) to the gate specified

Return type:

str

tequila_code.circuit.qasm.parse_command(command: str, custom_gates_map: Dict[str, QCircuit], qregisters: Dict[str, int]) QCircuit[source]

Parse qasm code command

Parameters:
  • command – open qasm code to be parsed

  • custom_gates_map – map with custom gates

tequila_code.circuit.qasm.parse_custom_gate(gate_custom: str, custom_gates_map: ~typing.Dict[str, ~tequila.circuit.circuit.QCircuit]) -> (<class 'str'>, <class 'tequila.circuit.circuit.QCircuit'>)[source]

Parse custom gates code

Parameters:

gate_custom – code with custom gates

tequila_code.circuit.qasm.parse_from_open_qasm_2(qasm_code: str, rigorous: bool = True) QCircuit[source]

qpic module

Export QCircuits as qpic files https://github.com/qpic/qpic/blob/master/doc/qpic_doc.pdf

tequila_code.circuit.qpic.assign_name(parameter)[source]
tequila_code.circuit.qpic.export_to(circuit, filename: str, style='tequila', qubit_names: list = None, *args, **kwargs)[source]
Parameters:
  • circuit – the tequila circuit to export

  • filename – filename.filetype, e.g. my_circuit.pdf, my_circuit.png (everything that qpic supports)

  • style – string keyword (tequila, standard, generators) or dictionary containing the following keys: always_use_generators: represent all gates with their generators decompose_control_generators: Decompose the controls to generators. Effective only in combination with always_use_generators=True. group_together: Keep PauliStrings from the same generator together. Effective only in combination with always_use_generators=True. possible values: False, True, ‘TOUCH’ and ‘BARRIER’. True is the same as TOUCH. BARRIER will create a visible barrier in qpic

  • args

  • kwargs

tequila_code.circuit.qpic.export_to_qpic(circuit, filename=None, filepath=None, always_use_generators=True, decompose_control_generators=False, group_together=False, qubit_names=None, mark_parametrized_gates=True, gatecolor1='tq', textcolor1='white', gatecolor2='guo', textcolor2='black', *args, **kwargs) str[source]

Module contents

tequila_code.circuit.compile_circuit(U, *args, **kwargs)[source]