Operations¶
Parent class of all primitive quantum operations. |
|
A blocking operation. |
|
Represents a unitary quantum gate. |
|
A measurement of a single qubit in the computational basis. |
|
A subclass of |
|
A preparation of a single qubit into the ground state of the computational basis. |
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]])
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:
-
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', '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']
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:
-
- 'IX': 90.0
- 'ZI': 90.0
- 'ZX': -90.0
- Matrix:
-
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.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, 'sx': Gate.sx, '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
, orH
. Gates in this dictionary can be accessed usingtrueq.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:
-
- 'IX': 90.0
- 'ZI': 90.0
- 'ZX': -90.0
- Matrix:
-
-
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 adict
.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(XX, YY)
- Likeness:
-
- CNOT
- Generators:
-
- 'XX': 90.0
- 'YY': 180.0
- Matrix:
-
Note
For rotations around a single Pauli term, it is faster to use
rp()
.- Parameters
args – A dictionary mapping Pauli strings to angles in degrees, or an alternating sequence thereof.
- Return type
-
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
-
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
-
static
random
(dim)¶ Generate a Haar random unitary gate of dimension
dim
.- Parameters
dim (
int
) – The dimension of the gate.- Return type
-
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.
-
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
-
static
rx
(angle)¶ Construct 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
-
static
ry
(angle)¶ Construct 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
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.name
is a string giving a name to the gate.(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
GateFactory
s, which are usually owned byConfig
objects.import trueq as tq config = tq.Config.basic() config.X(90)
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:
-
- X(phi)
- Aliases:
-
- Gate.sx
- Gate.cliff5
- Parameters:
-
- phi = 90
- Generators:
-
- 'X': 90.0
- Matrix:
-
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
) – SeeGate
.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 adict
.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:
-
- Parameters
name (
str
) – Name of the gate operation.args – A 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
-
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.