Qinterpreter
Overview
The OperatorBase class is a base class inherited by both the ObservableBase class and the
GateBase class. Instantiated subclasses of these two classes are building blocks to implement the quantum circuit in the TeD-Q package.
Each
GateBasesubclass represents a quantum gate. Gate parameter(s) (like theta and phi) and the qubit(s) this gate acts on are needed to instantiate it.Each
ObservableBasesubclass represents an observable that needs to be measured. Observable parameter(s) (like theta and phi) and the measurement qubit(s) are required to form a further measurement process.
Gates and Measurements
Abstract Base Class
This module contains the abstract base classes for defining tedq quantum gates and observables.
- class tedq.QInterpreter.operators.ops_abc.GateBase(*params, qubits: list, do_queue: bool = True, **kwargs)
Base class for quantum gate operator.
- Args:
params (tuple[float]): operator parameters
- Keyword Args:
qubits (list[int]): Which qubits this operator acts on. Error will occur if not given. do_queue (bool): Optional, defaul is True. Indicates whether this operator will appear in the quantum circuit.
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- get_parameter_shift()
Multiplier and shift for the given parameter. By default, differential can be obtain as: \(∂f(phi) = m*[f(phi+shift) - f(phi-shift)]\)
- Returns:
list[list[float]]
- property grad_style
Supported gradient computation style. This function need to be overwrited if it do not support ‘param-shift’
'param-shift': Parameter-shift method. Slow but accurate.'fint-diff': Numerical differentiation using finite difference. Slow but can be used for all gates.None: This quantum gate may not be differentiated.
- inverse()
Let this gate instace become an inverse gate. This function will change the name as well as the matrix of this instance
Example:
>>> import tedq as qai >>> A = qai.RZ(0.5, qubits = [1]) >>> A.name RZ >>> A.matrix array([[0.96891242 - 0.j, 0. + 0.24740396j], [0. + 0.24740396j, 0.96891242 - 0.j]]) >>> A.inverse() >>> A.name RZ.inv >>> A.matrix array([[0.96891242 + 0.j, 0. - 0.24740396j], [0. - 0.24740396j, 0.96891242 + 0.j]])
- property is_inverse
If the inverse of the operation was requested.
- Returns:
bool: The Gate is inverse or not.
- class tedq.QInterpreter.operators.ops_abc.ObservableBase(*params, qubits, do_queue=True, **kwargs)
Base class for quantum observables.
- Args:
params (tuple[float]): operator parameters
- Keyword Args:
qubits (list[int]): Which qubits this operator acts on. Error will occur if not given. do_queue (bool): Optional, defaul is True. Indicates whether this operator will appear in the quantum circuit.
- diagonalizing_gates()
Returns the list of gates in which they can diagonalize the observable in the computational basis.
- Returns:
list[~.GateBase]: A list of gates that diagonalize the observable in the computational basis.
- property eigvals
Eigenvalues of an observable instance.
Example:
>>> import tedq as qai >>> A = qai.PauliZ(qubits = [1]) >>> A.eigvals array([1, -1])
- Returns:
list[float]: eigvals representation
- class tedq.QInterpreter.operators.ops_abc.OperatorBase(*params, qubits, do_queue=True, **kwargs)
Abstract Base Class for quantum operators (measurement observables preparation, gates operation).
- Args:
params (tuple[float]): operator parameters
- Keyword Args:
qubits (list[int]): Which qubits this operator acts on. Error will occur if not given. do_queue (bool): Optional, defaul is True. Indicates whether this operator will appear in the quantum circuit.
- property eigvals
Eigenvalues of an operator instance.
Example:
>>> import tedq as qai >>> A = qai.RZ(0.5, qubits = [1]) >>> A.eigvals array([0.96891242-0.24740396j, 0.96891242+0.24740396j])
- Returns:
array: eigvals representation.
- get_matrix()
Get the matrix representation of an operator instance in computational basis.
- Returns:
list[float]: matrix representation.
- property instance_id
A sequencial unique id of this operator instance.
- Returns:
int: unique id of the operator.
- property is_preparation
If the operator used for the state preparation.
- Returns:
Bool: If the operator used for the state preparation.
- property matrix
Matrix representation of an operator instance in computational basis.
Example:
>>> import tedq as qai >>> A = qai.RY(0.5, qubits = [1]) >>> A.matrix array([[ 0.96891242+0.j, -0.24740396+0.j], [ 0.24740396+0.j, 0.96891242+0.j]])
- Returns:
list[float]: matrix representation.
- property name
Get the name of the operator.
- Returns:
String: The name of the operator
- property num_params
Get Numbers of paramters.
- Returns:
int: Number of parameters of this operator.
- property num_qubits
Get Numbers of qubits.
- Returns:
int: Number of qubits this operator acts on.
- property parameters
Get parameter values.
- Returns:
list[float]: List of parameters.
- property qubits
Get the qubits this operator acts on.
- Returns:
list[int]: the qubits this operator acts on.
- property trainable_params
Get the list of trainable parameters.
- Returns:
list[int]: the list of trainable parameters.
Qauntum Gates
This module contains the available quantum gates and observables supported by QInterpreter, as well as their simple properties.
- class tedq.QInterpreter.operators.qubit.CNOT(qubits)
The controlled-NOT (CX) gate. The matrix form is:
\[\begin{split}\begin{align} CNOT = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0 \end{bmatrix} \end{align}\end{split}\]Note
The first qubit is the control qubit.
- Properties:
# of qubits: 2
# of parameters: 0
- Args:
qubits (List[int]): The qubits the operation acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.CRX(*params, qubits: list, do_queue: bool = True, **kwargs)
controlled-RX gate. The matrix form is:
\[\begin{split}\begin{align} CR_x(\phi) = \begin{bmatrix} & 1 & 0 & 0 & 0 \\ & 0 & 1 & 0 & 0\\ & 0 & 0 & \cos(\phi/2) & -i\sin(\phi/2)\\ & 0 & 0 & -i\sin(\phi/2) & \cos(\phi/2) \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 2
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int]): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(theta, qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- get_parameter_shift()
Multiplier and shift for the given parameter. By default, differential can be obtain as: \(\frac{d}{d\phi}f(\phi) = m1 \left[f(\phi+SHIFT1) - f(\phi-SHIFT1)\right] - m2 \left[f(\phi+SHIFT2) - f(\phi-SHIFT2)\right]\)
- Returns:
list[list[float]]
- class tedq.QInterpreter.operators.qubit.CRY(*params, qubits: list, do_queue: bool = True, **kwargs)
controlled-RY gate. The matrix form is:
\[\begin{split}\begin{align} CR_y(\phi) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & \cos(\phi/2) & -\sin(\phi/2)\\ 0 & 0 & \sin(\phi/2) & \cos(\phi/2) \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 2
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int]): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(theta, qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- get_parameter_shift()
Multiplier and shift for the given parameter. By default, differential can be obtain as: \(\frac{d}{d\phi}f(\phi) = m1 \left[f(\phi+SHIFT1) - f(\phi-SHIFT1)\right] - m2 \left[f(\phi+SHIFT2) - f(\phi-SHIFT2)\right]\)
- Returns:
list[list[float]]
- class tedq.QInterpreter.operators.qubit.CRZ(*params, qubits: list, do_queue: bool = True, **kwargs)
controlled-RZ gate. The matrix form is:
\[\begin{split}\begin{align} CR_z(\phi) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & e^{-i\phi/2} & 0\\ 0 & 0 & 0 & e^{i\phi/2} \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 2
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int]): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(lam, qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- get_parameter_shift()
Multiplier and shift for the given parameter. By default, differential can be obtain as: \(\frac{d}{d\phi}f(\phi) = m1 \left[f(\phi+SHIFT1) - f(\phi-SHIFT1)\right] - m2 \left[f(\phi+SHIFT2) - f(\phi-SHIFT2)\right]\)
- Returns:
list[list[float]]
- class tedq.QInterpreter.operators.qubit.CSWAP(qubits)
The controlled-swap gate. The matrix form is:
\[\begin{split}\begin{align} CSWAP = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix} \end{align}\end{split}\]Note
The first qubit is the control qubit.
- Properties:
# of qubits: 3
# of parameters: 0
- Args:
qubits (List[int]): The qubits the operation acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.CY(qubits)
The controlled-Y gate. The matrix form is:
\[\begin{split}\begin{align} CY = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & -i\\ 0 & 0 & i & 0 \end{bmatrix} \end{align}\end{split}\]Note
The first qubit is the control qubit.
- Properties:
# of qubits: 2
# of parameters: 0
- Args:
qubits (List[int]): The qubits the operation acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.CZ(qubits)
The controlled-Z gate. The matrix form is:
\[\begin{split}\begin{align} CZ = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{bmatrix} \end{align}\end{split}\]Note
The first qubit is the control qubit.
- Properties:
# of qubits: 2
# of parameters: 0
- Args:
qubits (List[int]): The qubits the operation acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.ControlledPhaseShift(*params, qubits: list, do_queue: bool = True, **kwargs)
ControlledPhaseShift gate. The matrix form is:
\[\begin{split}\begin{align} CR_\phi(\phi) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i\phi} \end{bmatrix} \end{align}\end{split}\]Note
The first qubit is the control qubit.
- Properties:
# of qubits: 2
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int]): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(phi, qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.Hadamard(qubits)
Hadamard gate operator. The matrix form is:
\[\begin{split}\begin{align} H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \end{align}\end{split}\]- Properties
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int]): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- diagonalizing_gates(do_queue=False)
Return list of unitary operators that can diagonalize this gate. It can be used to rotate the specified qubits so that let the state in the eigenbasis of the Hadamard operator.
For the Hadamard operator,
\[H = U^\dagger H U\]where \(U = R_y(-\pi/4)\).
\[\langle \mathbf{\alpha} \vert H \vert \mathbf{\beta} \rangle = \langle \mathbf{\alpha} \vert U U^\dagger H U U^\dagger \vert \mathbf{\beta} \rangle\]- Returns:
list(~.GateBase): A list of operators that diagonalize Hadamard in the computational basis.
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.I(qubits)
The Identiy operator. The matrix form is:
\[\begin{split}\begin{align} I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- diagonalizing_gates(do_queue=False)
Returns the list of gates in which they can diagonalize the observable in the computational basis.
- Returns:
list[~.GateBase]: A list of gates that diagonalize the observable in the computational basis.
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.PauliX(qubits)
The Pauli X operator. The matrix form is:
\[\begin{split}\begin{align} \sigma_x = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1 # of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- diagonalizing_gates(do_queue=False)
Return list of unitary operators that can diagonalize this gate. It can be used to rotate the specified qubits so that let the state in the eigenbasis of the Hadamard operator.
For the Pauli-X operator,
\[X = U^\dagger X U\]where \(U = H\).
\[\langle \mathbf{\alpha} \vert X \vert \mathbf{\beta} \rangle = \langle \mathbf{\alpha} \vert U U^\dagger X U U^\dagger \vert \mathbf{\beta} \rangle\]- Returns:
list(~.GateBase): A list of operators that diagonalize Hadamard in the computational basis.
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.PauliY(qubits)
The Pauli Y operator. The matrix form is:
\[\begin{split}\begin{align} \sigma_y = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- diagonalizing_gates(do_queue=False)
Return list of unitary operators that can diagonalize this gate. It can be used to rotate the specified qubits so that let the state in the eigenbasis of the Hadamard operator.
For the Pauli-Y operator,
\[Y = U^\dagger Y U\]where \(U = HSZ\).
\[\langle \mathbf{\alpha} \vert Y \vert \mathbf{\beta} \rangle = \langle \mathbf{\alpha} \vert U U^\dagger Y U U^\dagger \vert \mathbf{\beta} \rangle\]- Returns:
list(~.GateBase): A list of operators that diagonalize Hadamard in the computational basis.
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.PauliZ(qubits)
The Pauli Z operator. The matrix form is:
\[\begin{split}\begin{align} \sigma_z = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- diagonalizing_gates(do_queue=False)
Return list of unitary operators that can diagonalize this gate. It can be used to rotate the specified qubits so that let the state in the eigenbasis of the Hadamard operator.
For the Pauli-Z operator,
\[Z = U^\dagger Z U\]where \(U = I\).
\[\langle \mathbf{\alpha} \vert Z \vert \mathbf{\beta} \rangle = \langle \mathbf{\alpha} \vert U U^\dagger Z U U^\dagger \vert \mathbf{\beta} \rangle\]- Returns:
list(~.GateBase): A list of operators that diagonalize Hadamard in the computational basis.
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.PhaseShift(phi, wires)
Arbitrary single qubit local phase shift gate. The matrix form is:
\[\begin{split}\begin{align} R_\phi(\phi) = e^{i\phi/2}R_z(\phi) = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\phi} \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(phi, qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.RX(*params, qubits: list, do_queue: bool = True, **kwargs)
The single qubit X rotation gate. The matrix form is:
\[\begin{split}\begin{align} R_x(\phi) = e^{-i\phi\sigma_x/2} = \begin{bmatrix} \cos(\phi/2) & -i\sin(\phi/2) \\ -i\sin(\phi/2) & \cos(\phi/2) \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.RY(*params, qubits: list, do_queue: bool = True, **kwargs)
The single qubit Y rotation gate. The matrix form is:
\[\begin{split}\begin{align} R_y(\phi) = e^{-i\phi\sigma_y/2} = \begin{bmatrix} \cos(\phi/2) & -\sin(\phi/2) \\ \sin(\phi/2) & \cos(\phi/2) \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.RZ(*params, qubits: list, do_queue: bool = True, **kwargs)
The single qubit Z rotation gate. The matrix form is:
\[\begin{split}\begin{align} R_z(\phi) = e^{-i\phi\sigma_z/2} = \begin{bmatrix} e^{-i\phi/2} & 0 \\ 0 & e^{i\phi/2} \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 1
- Args:
phi (float): rotation angle \(\phi\) qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- property generator
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.Rot(*params, qubits: list, do_queue: bool = True, **kwargs)
Arbitrary single qubit rotation gate. The matrix form is:
\[\begin{split}\begin{align} R(\phi,\theta,\omega) = RZ(\omega)RY(\theta)RZ(\phi)= \begin{bmatrix} e^{-i(\phi+\omega)/2}\cos(\theta/2) & -e^{i(\phi-\omega)/2}\sin(\theta/2) \\ e^{-i(\phi-\omega)/2}\sin(\theta/2) & e^{i(\phi+\omega)/2}\cos(\theta/2) \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 3
- Args:
phi (float): rotation angle \(\phi\) theta (float): rotation angle \(\theta\) omega (float): rotation angle \(\omega\) qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(phi, theta, omega, qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.S(qubits)
The single-qubit phase gate. The matrix form is:
\[\begin{split}\begin{align} S = \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.SWAP(qubits)
The swap gate. The matrix form is:
\[\begin{split}\begin{align} SWAP = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 2
# of parameters: 0
- Args:
qubits (List[int]): The qubits the operation acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.SX(qubits)
The single-qubit Square-Root X gate. The matrix form is:
\[\begin{split}\begin{align} SX = \sqrt{X} = \frac{1}{2} \begin{bmatrix} 1+i & 1-i \\ 1-i & 1+i \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.T(qubits)
The single-qubit T gate. The matrix form is:
\[\begin{split}\begin{align} T = \begin{bmatrix} 1 & 0 \\ 0 & e^{\frac{i\pi}{4}} \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 1
# of parameters: 0
- Args:
qubits (List[int] or int): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- static decomposition(qubits, do_queue=False)
Decompose this operator into products of other operators.
- Args:
params (tuple[float, int, array]): operator parameters qubits (list[int]): qubits the operator acts on do_queue (bool): wether put the decomposed operators into queuing.
- Returns:
list[~.GateBase]
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.Toffoli(qubits)
Toffoli (controlled-controlled-X) gate. The matrix form is:
\[\begin{split}\begin{align} Toffoli = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \end{bmatrix} \end{align}\end{split}\]- Properties:
# of qubits: 3
# of parameters: 0
- Args:
qubits (List[int]): the subsystem the gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- get_matrix()
Get the matrix representation of an operator instance in computational basis
- class tedq.QInterpreter.operators.qubit.Unitary(qubits)
Unitary gate operator. The matrix form is:
- Properties
# of qubits: 1~
# of parameters: 0
- Args:
qubits (List[int]): The qubits this gate acts on
- adjoint(do_queue=False)
Get the adjoint operator of this gate operator.
Adjoint operator tensor is the transposed conjugated tensor of the original gate operator tensor. For unitary gate operator, the tensor of adjoint operator is equivalent to the inverse tensor of original gate operator tensor.
- Args:
do_queue (bool): Optional, default is False. Indicates whether this operator will appear in the quantum circuit.
- Returns:
GateBase: the adjoint gate
- diagonalizing_gates(do_queue=False)
Return list of unitary operators that can diagonalize this gate. It can be used to rotate the specified qubits so that let the state in the eigenbasis of the Hadamard operator.
For the Pauli-X operator,
\[X = U^\dagger X U\]where \(U = H\).
\[\langle \mathbf{\alpha} \vert X \vert \mathbf{\beta} \rangle = \langle \mathbf{\alpha} \vert U U^\dagger X U U^\dagger \vert \mathbf{\beta} \rangle\]- Returns:
list(~.GateBase): A list of operators that diagonalize Hadamard in the computational basis.
- property eigvals
Eigenvalues of an operator instance.
Example:
>>> import tedq as qai >>> A = qai.RZ(0.5, qubits = [1]) >>> A.eigvals array([0.96891242-0.24740396j, 0.96891242+0.24740396j])
- Returns:
array: eigvals representation.
- get_matrix()
Get the matrix representation of an operator instance in computational basis
Quantum Mearsurements
This module contains the available measurement processes supported by QInterpreter (expectation, probability, state).
- class tedq.QInterpreter.operators.measurement.MeasurementReturnTypes(value)
The class define the Enums for the different return types of measurement.
- Expectation = 'expval'
- Probability = 'probs'
- Sample = 'sample'
- State = 'state'
- Variance = 'var'
- class tedq.QInterpreter.operators.measurement.QuantumMeasurement(return_type, obs=None, qubits=None, do_queue=True, after_state=False)
A measurement process at the end of quantum circuit. Three types of measurement (expectation, probability, state) are available by QInterpreter.
- Args:
return_type (.MeasurementReturnTypes): Which type of measurement is going to apply, available types are:
Expectation,ProbabilityandState. obs (.ObservableBase): Optional, default isNone. The observable that is to be measured. qubits (int): Optional, default isNone. The qubit(s) that the measurement applied to. Ifobsis notNone, this can not be specified.
- circuit_queue()
Append the measurement into the end of a quantum circuit.
- tedq.QInterpreter.operators.measurement.expval(observable, do_queue=True)
Compute expectation value measurement result of the input observable.
- Args:
observable (ObservableBase): An observable class which to be measured.
- Raises:
QuantumValueError: observable is not an ObservableBase class.
Example
def circuitDef(*params): qai.RX(params[0], qubits=[1]) qai.Hadamard(qubits=[0]) qai.CNOT(qubits=[0,1]) qai.RY(params[1], qubits=[0]) return qai.expval(qai.PauliZ(qubits=[0])) # for ZZ measurement #return qai.expval([qai.PauliZ(qubits=[0]), qai.PauliZ(qubits=[1])]) #compile the quantum circuit circuit = qai.Circuit(circuitDef, 2, 0.54, 0.12) my_compilecircuit = circuit.compilecircuit(backend="jax")
Execute the circuit and obtain the result:
>>> my_compilecircuit(0.54, 0.12) [DeviceArray(-9.4627275e-09, dtype=float32)]
- tedq.QInterpreter.operators.measurement.probs(qubits=None, do_queue=True, after_state=False)
Compute the probability of each computational basis state
//TODO: some description and fix the example
Example
def circuitDef(*params): qai.RX(params[0], qubits=[1]) qai.Hadamard(qubits=[0]) qai.CNOT(qubits=[0,1]) qai.RY(params[1], qubits=[0]) return qai.probs(qai.PauliZ(qubits=[0])) #compile the quantum circuit circuit = qai.Circuit(circuitDef, 2, 0.54, 0.12) my_compilecircuit = circuit.compilecircuit(backend="jax")
Execute the circuit and obtain the result:
>>> my_compilecircuit(0.54, 0.12) [DeviceArray(-9.4627275e-09, dtype=float32)]
The returned array is in lexicographic order, so corresponds to a \(50\%\) chance of measuring either \(|00\rangle\) or \(|01\rangle\).
- Args:
wires (Sequence[int] or int): the wire the operation acts on
- tedq.QInterpreter.operators.measurement.sample(observable, num_shots, do_queue=True)
Obtain the sample of measurement for the supplied observable.
- tedq.QInterpreter.operators.measurement.state(do_queue=True)
Quantum state in the computational basis.
//TODO: some description and fix the example
Example
def circuitDef(*params): qai.RX(params[0], qubits=[1]) qai.Hadamard(qubits=[0]) qai.CNOT(qubits=[0,1]) qai.RY(params[1], qubits=[0]) return qai.probs(qai.PauliZ(qubits=[0])) #compile the quantum circuit circuit = qai.Circuit(circuitDef, 2, 0.54, 0.12) my_compilecircuit = circuit.compilecircuit(backend="jax")
Execute the circuit and obtain the result:
>>> my_compilecircuit(0.54, 0.12) [DeviceArray(-9.4627275e-09, dtype=float32)]
- tedq.QInterpreter.operators.measurement.var(observable, do_queue=True)
Obtain the variance of measurement for the supplied observable.