Operations

trueq.Operation

Parent class of all primitive quantum operations.

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.

Gate

class trueq.Gate(u)

Represents a unitary quantum gate.

import trueq as tq

tq.Gate([[0, 1], [1, 0]])
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
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', 'sx', 'sy', 't', 'ch', 'i', 'sz', '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']
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Name:
  • Gate.cx
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'ZX': -90.0
  • 'IX': 90.0
  • 'ZI': 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.sx, 'cliff6': Gate.cliff6, 'cliff7': Gate.sy, '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, 'sx': Gate.sx, 'sy': Gate.sy, 'sz': Gate.s, '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
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Name:
  • Gate.cx
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'ZX': -90.0
  • 'IX': 90.0
  • 'ZI': 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
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Name:
  • Gate(YY, XX)
Likeness:
  • CNOT
Generators:
  • 'YY': 180.0
  • 'XX': 90.0
Matrix:
  • 0.71 0.71j -0.71 -0.71j -0.71j -0.71 0.71j 0.71

Note

For rotations around a single Pauli term, it is faster to use rp().

Parameters

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

Return type

Gate

get_alias(default=None)

Returns the first matching alias of the gate if possible, or default if no aliases are found.

Parameters

default (NoneType | str) – A string to return if no alias is found for this gate. If no string is specified, None will be returned.

Returns

The alias of this gate if it exists, default otherwise.

Return type

str | NoneType

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

A sparse representation of this gate in terms of the Weyl operators; when this gate acts on qubits, 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.

Parameters

alias (str) – A string representing a common gate name found in the ALIASES dictionary, which stores available gates.

Return type

Gate

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
import numpy as np

gens = tq.Gate.from_generators("X", 90, "Z", 20).generators
assert np.isclose(gens["X"], 90)
assert np.isclose(gens["Z"], 20)
Type

dict

property adj

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

Type

Gate

property sqrt

A new Gate instance which is a square root of this gate, respecting the global phase. That is, this method guarantees that \(U^{1/2}U^{1/2}=U\) for any unitary gate \(U\). This differs from the general power method on Gate which chooses a certain scheme for global phases tailored for overrotation errors.

Type

Gate

controlled(control_on='1', pos=- 1)

A new Gate instance which is a controlled version of this gate. The gate is applied to the qudit(s) in positions pos if and only if the rest of the qudits are in the computational basis state control_on.

import trueq as tq

# Toffoli gate
gate = tq.Gate.x
gate.controlled('11')
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Name:
  • Gate(ZZX, ZIX, ...)
Generators:
  • 'ZZX': 45.0
  • 'ZIX': -45.0
  • 'IZX': -45.0
  • 'IIX': 45.0
  • 'ZZI': -45.0
  • 'ZII': 45.0
  • 'IZI': 45.0
Matrix:
  • 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
Parameters
  • control_on (Iterable | str) – The ditstring specifying the computational state to condition on. This can be any iterable whose elements are castable to integers representing dits. The default setting conditions on the computational basis state \(|1\rangle\).

  • pos (int | tuple) – The position of the subsystem on which the controlled gate acts. The default is the last subsystem.

Return type

Gate

embed(extra_dim, n_sys=None)

Returns a Gate that is equal to this gate embedded in a larger Hilbert space. That is, if this gate acts on the space \((\mathbb{C}^d)^{\otimes n}\), then the returned gate will act on the space \((\mathbb{C}^{d+d'})^{\otimes n}\).

Parameters
  • extra_dim (int) – The number of extra dimensions, \(d'\), to add to each subsystem.

  • n_sys (int | NoneType) – The number of subsystems that this gate acts on. If unspecified, it is inferred automatically by asuming \(d\) is prime, and if \(d\) cannot be prime (i.e. the total dimension is not a prime power), then n_sys=1 is used.

Return 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 (NoneType | 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.

static rp(pauli, angle)

Construct a gate from a Pauli rotation by the specified angle.

import trueq as tq
import numpy as np

gens = tq.Gate.rp("XX", 10).generators
assert np.isclose(gens['XX'], 10)

Note

For single pauli rotations this is faster numerically than from_generators().

Parameters
  • pauli (str) – The Pauli axis of rotation.

  • angle (float) – The angle of the rotation in degrees.

Return type

Gate

static rx(angle)

Constructs a gate from a Pauli X rotation by the specified angle.

import trueq as tq
import numpy as np

gens = tq.Gate.rx(10).generators
assert np.isclose(gens['X'], 10)
Parameters

angle (float) – The angle of the rotation in degrees.

Return type

Gate

static ry(angle)

Constructs a gate from a Pauli Y rotation by the specified angle.

import trueq as tq
import numpy as np

gens = tq.Gate.ry(10).generators
assert np.isclose(gens['Y'], 10)
Parameters

angle (float) – The angle of the rotation in degrees.

Return type

Gate

static rz(angle)

Constructs a gate from a Pauli Z rotation by the specified angle.

import trueq as tq
import numpy as np

gens = tq.Gate.rz(10).generators
assert np.isclose(gens['Z'], 10)
Parameters

angle (float) – The angle of the rotation in degrees.

Return type

Gate

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)

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.

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()
config.sx()
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
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.

static from_generators(name, *args, params=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
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Name:
  • name(theta)
Aliases:
  • Gate.sx
  • 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.

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

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

Return type

NativeGate

property parameters

The stored parameters.

Type

dict

property name

The name of this native gate.

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.