qsearch.gates

This module defines the Gate class, which represents a quantum gate, as well as implementations of many common Gates.

Through the use of KroneckerGate and ProductGate, Gates can be formed for complex circuit structures. The matrix and mat_jac functions are used for numerical optimization of parameterized gates. The assemble function is used to generate an intermediate language of tuples that can be used by Assemblers to output descriptions of quantum circuits in other formats.

Module Contents

Classes

Gate

This class shows the framework for working with quantum gates in Qsearch.

IdentityGate

Represents an identity gate of any number of qudits of any size.

XGate

Represents a parameterized X rotation on one qubit.

YGate

Represents a parameterized Y rotation on one qubit.

ZGate

Represents a parameterized Z rotation on one qubit.

SXGate

Represents a sqrt(X) rotation on one qubit, which is equivalent to XGate() with a paramter of pi/2, up to an overall phase.

ZXZXZGate

Represents an arbitrary parameterized single-qubit gate, decomposed into 3 parameterized Z gates separated by X(PI/2) gates.

XZXZGate

Represents a partially parameterized single qubit gate, equivalent to ZXZXZ but without the first Z gate. This is useful because that first Z gate can commute through the control of a CNOT, thereby reducing the number of parameters we need to solve for.

U3Gate

Represents an arbitrary parameterized single qubit gate, parameterized in the same way as IBM's U3 gate.

U2Gate

Represents a parameterized single qubit gate, parameterized in the same way as IBM's U2 gate.

U1Gate

Represents an parameterized single qubit gate, parameterized in the same way as IBM's U1 gate.

SingleQutritGate

This gate represents an arbitrary parameterized single-qutrit gate.

CSUMGate

Represents the constant two-qutrit gate CSUM

CPIGate

Represents the constant two-qutrit gate CPI.

CPIPhaseGate

Represents the constant two-qutrit gate CPI with phase differences.

CNOTGate

Represents the constant two-qubit gate CNOT.

CZGate

Represents the constant two-qubit gate Controlled-Z.

ISwapGate

Represents the constant two-qubit gate ISwap.

XXGate

Represents the constant two-qubit gate XX(pi/2).

NonadjacentCNOTGate

Represents the two-qubit gate CNOT, but between two qubits that are not necessarily next to each other.

UGate

Represents an arbitrary constant gate, defined by the unitary passed to the initializer.

UpgradedConstantGate

Represents a constant gate, based on the Gate passed to its initializer, but upgraded to act on qudits of a larger size.

CUGate

Represents an arbitrary controlled gate, defined by the unitary passed to the initializer.

CNOTRootGate

Represents the sqrt(CNOT) gate. Two sqrt(CNOT) gates in a row will form a CNOT gate.

KroneckerGate

Represents the Kronecker product of a list of gates. This is equivalent to performing those gate in parallel in a quantum circuit.

ProductGate

Represents a matrix product of Gates. This is equivalent to performing those gates sequentially in a quantum circuit.

Attributes

native_from_object

qsearch.gates.native_from_object
class qsearch.gates.Gate

This class shows the framework for working with quantum gates in Qsearch.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

abstract matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

abstract assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__eq__(other)

Return self==value.

__hash__()

Return hash(self).

copy()
_parts()
__copy__()
__deepcopy__(memo)
__repr__()

Return repr(self).

validate_structure()
class qsearch.gates.IdentityGate(qudits=1, d=2)

Bases: Gate

Represents an identity gate of any number of qudits of any size.

Parameters:
  • qudits – The number of qudits represented by this identity.

  • d – The size of qudits represented by this identity (2 for qubits, 3 for qutrits, etc.)

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.XGate

Bases: Gate

Represents a parameterized X rotation on one qubit.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.YGate

Bases: Gate

Represents a parameterized Y rotation on one qubit.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.ZGate

Bases: Gate

Represents a parameterized Z rotation on one qubit.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.SXGate

Bases: Gate

Represents a sqrt(X) rotation on one qubit, which is equivalent to XGate() with a paramter of pi/2, up to an overall phase.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.ZXZXZGate

Bases: Gate

Represents an arbitrary parameterized single-qubit gate, decomposed into 3 parameterized Z gates separated by X(PI/2) gates.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.XZXZGate

Bases: Gate

Represents a partially parameterized single qubit gate, equivalent to ZXZXZ but without the first Z gate. This is useful because that first Z gate can commute through the control of a CNOT, thereby reducing the number of parameters we need to solve for.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.U3Gate

Bases: Gate

Represents an arbitrary parameterized single qubit gate, parameterized in the same way as IBM’s U3 gate.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class qsearch.gates.U2Gate

Bases: Gate

Represents a parameterized single qubit gate, parameterized in the same way as IBM’s U2 gate.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class qsearch.gates.U1Gate

Bases: Gate

Represents an parameterized single qubit gate, parameterized in the same way as IBM’s U1 gate.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class qsearch.gates.SingleQutritGate

Bases: Gate

This gate represents an arbitrary parameterized single-qutrit gate.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CSUMGate

Bases: Gate

Represents the constant two-qutrit gate CSUM

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_csum
matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CPIGate

Bases: Gate

Represents the constant two-qutrit gate CPI.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_cpi
matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CPIPhaseGate

Bases: Gate

Represents the constant two-qutrit gate CPI with phase differences.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CNOTGate

Bases: Gate

Represents the constant two-qubit gate CNOT.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_cnot
__eq__(other)

Return self==value.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CZGate

Bases: Gate

Represents the constant two-qubit gate Controlled-Z.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_gate
__eq__(other)

Return self==value.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.ISwapGate

Bases: Gate

Represents the constant two-qubit gate ISwap.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_gate
__eq__(other)

Return self==value.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.XXGate

Bases: Gate

Represents the constant two-qubit gate XX(pi/2).

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_gate
__eq__(other)

Return self==value.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.NonadjacentCNOTGate(qudits, control, target)

Bases: Gate

Represents the two-qubit gate CNOT, but between two qubits that are not necessarily next to each other.

Parameters:
  • qudits – The total number of qubits that a unitary of the size returned by this gate would represent. For this gate, usually this is the total number of qubits in the larger circuit.

  • control – The index of the control qubit, relative to the 0th qubit that would be affected by the unitary returned by this gate.

  • target – The index of the target qubit, relative to the 0th qubit that would be affected by the unitary returned by this gate.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

validate_structure()
class qsearch.gates.UGate(U, d=2, gatename='CUSTOM', gateparams=(), gateindices=None)

Bases: Gate

Represents an arbitrary constant gate, defined by the unitary passed to the initializer.

Parameters:
  • U – The unitary for the operation that this gate represents, as a numpy ndarray with datatype=”complex128”.

  • d – The size of qudits for the operation that this gate represents. The default is 2, for qubits.

  • gatename – A name for this gate, which will get passed to the Assembler at assembly time.

  • gateparams – A tuple of parameters that will get passed to the Assembler at assembly time.

  • gateindices – A tuple of indices for the qubits that this gate acts on, which will get passed to the Assembler at assembly time. This overrides the default behavior, which is to return a tuple of all the indices starting with the one passed in assemble(v, i), and ending at i+self.qudits

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.UpgradedConstantGate(other, df=3)

Bases: Gate

Represents a constant gate, based on the Gate passed to its initializer, but upgraded to act on qudits of a larger size.

Parameters:
  • other – A Gate of a lower qudit size.

  • df – The final, upgraded qudit size. The default is 3, for upgrading gates from qubits to qutrits.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CUGate(U, gatename='Name', gateparams=(), flipped=False)

Bases: Gate

Represents an arbitrary controlled gate, defined by the unitary passed to the initializer.

Parameters:
  • U – The unitary to form the controlled-unitary gate, in the form of a numpy ndarray with dtype=”complex128”

  • gatename – A name for this controlled gate which will get passed to the Assembler at assembly time.

  • gateparams – A tuple of parameters that will get passed to the Assembler at assembly time.

  • flipped – A boolean flag, which if set to true, will flip the direction of the gate. The default direction is for the control qubit to be the lower indexed qubit.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.CNOTRootGate

Bases: Gate

Represents the sqrt(CNOT) gate. Two sqrt(CNOT) gates in a row will form a CNOT gate.

Gates must set the following variables in __init__

self.num_inputs : The number of parameters needed to generate a unitary. This can be 0. self.qudits : The number of qudits acted on by a unitary of the size generated by the gate. For example, this would be 1 for U3, 2 for CNOT.

_cnr
matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

__repr__()

Return repr(self).

class qsearch.gates.KroneckerGate(*subgates)

Bases: Gate

Represents the Kronecker product of a list of gates. This is equivalent to performing those gate in parallel in a quantum circuit.

Parameters:

*subgates – An sequence of Gates. KroneckerGate will return the kronecker product of the unitaries returned by those Gates.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

appending(gate)

Returns a new KroneckerGate with the new gate added to the list.

Parameters:

gate – A Gate to be added to the end of the list of gates in the new KroneckerGate.

_parts()
__deepcopy__(memo)
__repr__()

Return repr(self).

validate_structure()
class qsearch.gates.ProductGate(*subgates)

Bases: Gate

Represents a matrix product of Gates. This is equivalent to performing those gates sequentially in a quantum circuit.

Parameters:

subgates – A list of Gates to be multiplied together. ProductGate returns the matrix product of the unitaries returned by those Gates.

matrix(v)

Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A unitary matrix with dtype=”complex128”, equal in size to d**self.qudits, where d is the intended qudit size (d is 2 for qubits, 3 for qutrits, etc.)

Return type:

np.ndarray

mat_jac(v)

Generates a matrix and the jacobian(s) using the given vector of input parameters.

It is not required to implement mat_jac for constant gates, nor is it required when using gradient-free Solvers.

The jacobian matrices will be complex valued, and should be the elementwise partial derivative with respect to each of the parameters. There should be self.num_inputs matrices in the array, with the ith entry being the partial derivative with respect to v[i]. See U3Gate for an example implementation.

Parameters:

v – A numpy array of real floating point numbers, ranging from 0 to 2*PI. Its size is equal to self.num_inputs

Returns:

A tuple of the same unitary that would be returned by matrix(v), and an array of Jacobian matrices.

Return type:

tuple

assemble(v, i=0)

Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats.

Parameters:
  • v – The same numpy array of real floating point numbers that might be passed to matrix(v).

  • i – The index of the lowest-indexed qubit that the unitary generated by the gate acts on.

Returns:

A list of tuples following the format described above.

Return type:

list

The format of the tuples returned looks like:

(“gate”, gatename, (*gateparameters), (*gateindices))

Where gatename corresponds to a gate that an Assembler will recognize, gateparameters corresponds to the parameters for the specified gate (usually but not always calculated from v), and gateindices corresponds to the qubit indices that the gate acts on (usually but not always calculated from i).

You can also have tuples of the form (“block”, *tuples) Where tuples is an array of tuples in this same format.

For some helpful examples, look at U3Gate, XZXZGate, CNOTGate, and NonadjacentCNOTGate.

appending(*gates)

Returns a new ProductGate with the new gates appended to the end.

Parameters:

gates – A list of Gates to be appended.

inserting(*gates, depth=-1)

Returns a new ProductGate with new gates inserted at some index depth.

Parameters:
  • gates – A list of Gates to be inserted.

  • depth – An index in the subgates of the ProductGate after which the new gates will be inserted. The default value of -1 will insert these gates at the begining of the ProductGate.

__deepcopy__(memo)
__repr__()

Return repr(self).

validate_structure()