Operations

trueq.Operation

Parent class of all primitive quantum operations.

trueq.Block

A blocking operation.

trueq.Gate

Represents a unitary quantum gate.

trueq.Meas

A measurement of a single qubit in the computational basis.

trueq.NativeGate

A subclass of Gate which introduces metadata attributes.

trueq.Prep

A preparation of a single qubit into the ground state of the computational basis.

Operation

class trueq.Operation

Parent class of all primitive quantum operations.

Block

class trueq.Block

A blocking operation.

This class holds no information, it allows a trueq.Circuit to keep track of where other operations are not allowed to be placed.

This is a singleton class; the constructor always yields the same instance.

Gate

class trueq.Gate(u)

Represents a unitary quantum gate.

import trueq as tq

tq.Gate([[0, 1], [1, 0]])
Name:
  • Gate.x
Aliases:
  • Gate.x
  • Gate.cliff1
Generators:
  • 'X': 180.0
Matrix:
  • 1.00 1.00

Many standard gates are predefined as attributes:

import trueq as tq

print("All available predefined gate names:")
print(list(tq.Gate.ALIASES))

# Accessing a predefined gate:
tq.Gate.cx
All available predefined gate names:
['id', 'x', 'y', 'z', 'cx', 'cy', 'cz', 'swap', 'iswap', 'h', 's', 't', 'ch', 'i', 'cnot', 'cliff0', 'cliff1', 'cliff2', 'cliff3', 'cliff4', 'cliff5', 'cliff6', 'cliff7', 'cliff8', 'cliff9', 'cliff10', 'cliff11', 'cliff12', 'cliff13', 'cliff14', 'cliff15', 'cliff16', 'cliff17', 'cliff18', 'cliff19', 'cliff20', 'cliff21', 'cliff22', 'cliff23']
Name:
  • Gate.cx
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 1.00 1.00 1.00 1.00

The static method from_generators() can be used to construct gates from Pauli rotations.

Note

Gates are not checked to be unitary on instantiation; call is_unitary manually to check.

Parameters

u (numpy.ndarray-like | dict) – A unitary matrix in the computational basis.

ALIASES = {'ch': Gate.ch, 'cliff0': Gate.id, 'cliff1': Gate.x, 'cliff10': Gate.cliff10, 'cliff11': Gate.cliff11, 'cliff12': Gate.h, 'cliff13': Gate.cliff13, 'cliff14': Gate.cliff14, 'cliff15': Gate.cliff15, 'cliff16': Gate.cliff16, 'cliff17': Gate.cliff17, 'cliff18': Gate.cliff18, 'cliff19': Gate.cliff19, 'cliff2': Gate.y, 'cliff20': Gate.cliff20, 'cliff21': Gate.cliff21, 'cliff22': Gate.cliff22, 'cliff23': Gate.cliff23, 'cliff3': Gate.z, 'cliff4': Gate.cliff4, 'cliff5': Gate.cliff5, 'cliff6': Gate.cliff6, 'cliff7': Gate.cliff7, 'cliff8': Gate.cliff8, 'cliff9': Gate.s, 'cnot': Gate.cx, 'cx': Gate.cx, 'cy': Gate.cy, 'cz': Gate.cz, 'h': Gate.h, 'i': Gate.id, 'id': Gate.id, 'iswap': Gate.iswap, 's': Gate.s, 'swap': Gate.swap, 't': Gate.t, 'x': Gate.x, 'y': Gate.y, 'z': Gate.z}

A dictionary containing pre-defined gates where the keys are the gate names, for example CNOT, or H. Gates in this dictionary can be accessed using trueq.Gate.<gate_name>, for example:

import trueq as tq
tq.Gate.cx
Name:
  • Gate.cx
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 1.00 1.00 1.00 1.00
static from_generators(*args)

Constructs a Gate instance from rotations about Paulis. Specifically, the gate is constructed as \(exp(-1j * \sum_i (\pi\theta_i/180) P_i/ 2)\) where \(P_i\) are the input Pauli strings and \(\theta_i\) are the input rotation angles in degrees. This can be input either as an alternating sequence of Pauli strings and angles, or as a dict.

import trueq as tq

g1 = tq.Gate.from_generators("X", 90)
g2 = tq.Gate.from_generators("XX", 90, "YY", 180)
g3 = tq.Gate.from_generators({"XX": 90, "YY": 180})

g2
Name:
  • Gate(XX, YY)
Likeness:
  • CNOT
Generators:
  • 'XX': 90.0
  • 'YY': 180.0
Matrix:
  • 0.71 0.71j -0.71 -0.71j -0.71j -0.71 0.71j 0.71
Parameters

args – A dictionary mapping Pauli strings to angles in degrees, or an alternating sequence thereof.

Return type

Gate

property is_unitary

Whether or not this gate is unitary to numerical precision.

Type

bool

property is_identity

Whether or not this gate is the identity gate to numerical precision.

Type

bool

property mat

The matrix representation of this gate in the canonical basis.

Type

numpy.ndarray

property width

The number of columns of the matrix representation of this gate.

Type

int

property expansion

Assuming this gate acts on qubits, a sparse representation of this gate in terms of the trueq.math.frame.pauli_basis; a dictionary mapping Pauli strings to (complex) coefficients.

Type

dict

static from_alias(alias)

Returns a Gate from common names, such as CX, swap, H, S etc.

ALIASES is a dictionary which stores available gates.

Raises

KeyError – If the gate is not found.

property generators

Assumes this gate acts on qubits and returns rotations about Pauli axes performed by this Gate. Specifically, outputs a description of this gate in the form: \(exp(-1j * \sum_i (\pi\theta_i/180) P_i/ 2)\) where \(P_i\) are the Pauli strings and \(\theta_i\) are the rotation angles in degrees.

import trueq as tq

tq.Gate.from_generators("X", 90, "Z", 20).generators
{"X": 90, "Z": 20}
{'X': 90, 'Z': 20}
Type

dict

property adj

A new Gate instance which is the conjugate transpose of this gate.

Type

Gate

static random(dim)

Generate a Haar random unitary gate of dimension dim.

Parameters

dim (int) – The dimension of the gate.

Return type

Gate

plot(abs_max=None, ax=None)

Plots the matrix representation of this gate.

Parameters
  • abs_max (None | float) – The value to scale absolute values of the matrix by; the value at which plotted colors become fully opaque. By default, this is the largest absolute magnitude of the input matrix.

  • ax (matplotlib.Axis) – An existing axis to plot on. If not given, a new figure will be created.

Measurement

class trueq.Meas

A measurement of a single qubit in the computational basis.

This class holds no information, it allows a trueq.Circuit to keep track of where a measurement has taken place.

This is a singleton class; the constructor always yields the same instance.

Native Gate

class trueq.NativeGate(name, u, params=None, classname=None)

A subclass of Gate which introduces metadata attributes.

  1. name is a string giving a name to the gate.

  2. (optional) parameters is a dictionary that maps parameter names to parameter values.

  3. (optional) classname is a string specifying the factory that constructed this instance.

These attributes are entirely metadata; there are no automatic consistency checks between them and the matrix representation. The purpose of these attributes is to provide tools, such as transpilers to third-party circuit formats, with a fast method of identifying gates without introspecting matrices. This requires trust that these metadata were generated correctly and in good faith.

Native gates are typically (and most easily) constructed by GateFactorys, which are usually owned by Config objects.

import trueq as tq

config = tq.Config.basic("example")
config.X(90)
Name:
  • example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71

Native gates are not intended to be mutable, and the parameters and names should not be changed after instantiation.

Parameters
  • name (str) – Name of the gate operation.

  • u (dict) – See Gate.

  • params (dict) – Any parameters that the gate needs in order to be constructed.

  • classname – The constructing class name of this native gate. This is usually assigned by a GateFactory on instantiation.

static from_generators(name, *args, params=None, classname=None)

Constructs a NativeGate instance from rotations about Paulis. Also adds a name and any parameters used to construct the native gate. Specifically, the output gate in this case is given by \(exp(-1j * \sum_i (\pi\theta_i/180) P_i/ 2)\) where \(P_i\) are the input Pauli strings and \(\theta_i\) are the input rotation angles in degrees. This can be input either as an alternating sequence of Pauli strings and angles, or as a dict.

import trueq as tq

g1 = tq.NativeGate.from_generators("name", "X", 90, params={"theta": 90})
g2 = tq.NativeGate.from_generators("name", "XX", 90, "YY", 180)
g3 = tq.NativeGate.from_generators("name", {"XX": 90, "YY": 180})

g1
Name:
  • name
Aliases:
  • Gate.cliff5
Parameters:
  • theta = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
Parameters
  • name (str) – Name of the gate operation.

  • params (dict) – Any parameters that the gate needs in order to be constructed.

  • args – A dictionary mapping Pauli strings to angles in degrees, or an alternating sequence thereof.

  • classname (str) – The constructing class name of this native gate. This is usually assigned by a GateFactory on instantiation.

Return type

NativeGate

property parameters

The stored parameters.

Type

dict

property name

The name of this native gate.

Type

str

property classname

The constructing class name of this native gate. This is usually assigned by a GateFactory on instantiation.

Type

str

Preparation

class trueq.Prep

A preparation of a single qubit into the ground state of the computational basis.

This class holds no information, it allows a trueq.Circuit to keep track of where a state preparation has taken place.

This is a singleton class; the constructor always yields the same instance.