:py:mod:`qsearch` ================= .. py:module:: qsearch Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 advanced_unitaries/index.rst assemblers/index.rst backends/index.rst checkpoints/index.rst comparison/index.rst compiler/index.rst defaults/index.rst evaluation/index.rst gates/index.rst gatesets/index.rst heuristics/index.rst integrations/index.rst leap_compiler/index.rst logging/index.rst multistart_solvers/index.rst objectives/index.rst options/index.rst parallelizers/index.rst persistent_aposmm/index.rst post_processing/index.rst project/index.rst solvers/index.rst unitaries/index.rst utils/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: qsearch.Options qsearch.Compiler qsearch.SearchCompiler qsearch.Gate qsearch.IdentityGate qsearch.XGate qsearch.YGate qsearch.ZGate qsearch.SXGate qsearch.ZXZXZGate qsearch.XZXZGate qsearch.U3Gate qsearch.U2Gate qsearch.U1Gate qsearch.SingleQutritGate qsearch.CSUMGate qsearch.CPIGate qsearch.CPIPhaseGate qsearch.CNOTGate qsearch.CZGate qsearch.ISwapGate qsearch.XXGate qsearch.NonadjacentCNOTGate qsearch.UGate qsearch.UpgradedConstantGate qsearch.CUGate qsearch.CNOTRootGate qsearch.KroneckerGate qsearch.ProductGate qsearch.Project Attributes ~~~~~~~~~~ .. autoapisummary:: qsearch.standard_defaults qsearch.standard_smart_defaults qsearch.native_from_object .. py:class:: Options(defaults={}, smart_defaults={}, **xtraargs) This class manages options that are passed between various Qsearch objects. .. py:method:: filtered(self, *names) Returns an Options object with only parameters in the specified list names. .. py:method:: __getitem__(self, name) .. py:method:: __delitem__(self, name) .. py:method:: __getattr__(self, name) .. py:method:: __setattr__(self, name, value) Implement setattr(self, name, value). .. py:method:: __contains__(self, name) .. py:method:: manually_entered(self, *names, location='dict', operator='all') .. py:method:: empty_copy(self) Create an Options object with the same defaults but without any specific values. .. py:method:: copy(self) Create a full copy of an Options object. .. py:method:: __copy__(self) .. py:method:: updated(self, other=None, **xtraargs) Return a new Options object that is a copy of this object, updated with the contents of other and xtraargs. .. py:method:: update(self, other=None, **xtraargs) Mutate the current Options object with the contents of other and xtraargs. .. py:method:: _update_dict(self, otherdict) .. py:method:: set_defaults(self, **args) Set default values for this Options object. If an Options object is queried for a value, and it does not contain it, it will check its defaults list before throwing an error. .. py:method:: set_smart_defaults(self, **args) Set smart_defaults values for this Options object. If an Options object is queried for a value, and it does not contain it, it will check its smart_defaults list before throwing an error. If it does find a function in smart_defaults, it calls that function, passing itself as the argument, and returns the return value of that function, caching it for next time. .. py:method:: make_required(self, *names) Marking names as required will cause the Options object to throw an error if it does not contain it, even if it has defaults or smart_defaults defined. .. py:method:: remove_defaults(self, *names) Removes the defaults for the specified names. .. py:method:: remove_smart_defaults(self, *names) Removes the smart_defaults for the specified names. .. py:method:: generate_cache(self) Caches valuesa for all functions in smart_defaults. .. py:method:: save(self, filepath=None) Saves the Options object to a file, or to a returned tuple. .. py:method:: load(self, filepath_or_tuple, strict=False) Loads the Options object from a file or tuple. If strict is left as False, the Options object will attempt to gracefully handle errors when loading its contents, relying on its ability to fallback to defaults or smart_defaults if those are able to load successfully. If strict is set to True, the Options object will throw an error upon any error while loading. .. py:method:: __getstate__(self) .. py:method:: __setstate__(self, state) .. py:data:: standard_defaults .. py:data:: standard_smart_defaults .. py:class:: Compiler(options=Options()) This class defines the pattern for compilers that convert a unitary matrix to a circuit that implements that matrix. .. py:method:: compile(self, options) :abstractmethod: .. py:class:: SearchCompiler(options=Options()) Bases: :py:obj:`Compiler` This Compiler uses an A* search strategy to synthesize a unitary, as described in the paper Towards Optimal Topology Aware Quantum Circuit Synthesis. Options: target (required) : The unitary matrix to be synthesized, in the form of a numpy ndarray with dtype="complex128". gateset : The Gateset used for synthesis. weight_limit : A limit on the maximum weight for circuits to be expanded for further searching. See gatesets.py for more information. The default is None for unlimited. heuristic : A heuristic used to order the search tree. See heuristics.py for more information. solver : A Solver used for optimizing the parameters in parameterized circuits generated by the search tree. parallelizer : A Parallelizer used for solving multiple parameterized circuits in parallel. beams : The number of nodes to pop from the search tree at a time. The default value of -1 will create enough branches to maximize utilization of your CPU. objective : An Objective used for scoring the quality of a parameterization for both synthesis and search. timeout : An uper limit on the amount of time the compiler will spend trying to synthesize a circuit. The default is float('inf'), for unlimited. checkpoint : The compiler will use this Checkpoint to save intermediate state, and will resume from this Checkpoint if there was an existing state. logger : A qsearch.logging.Logger that will be used for logging the synthesis process. :param options: See class level documentation for the options SearchCompiler uses .. py:method:: compile(self, options=Options()) :param options: See class level documentation for the options SearchCompiler uses .. py:data:: native_from_object .. py:class:: 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. .. py:method:: matrix(self, v) :abstractmethod: Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) :abstractmethod: Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __eq__(self, other) Return self==value. .. py:method:: __hash__(self) Return hash(self). .. py:method:: copy(self) .. py:method:: _parts(self) .. py:method:: __copy__(self) .. py:method:: __deepcopy__(self, memo) .. py:method:: __repr__(self) Return repr(self). .. py:method:: validate_structure(self) .. py:class:: IdentityGate(qudits=1, d=2) Bases: :py:obj:`Gate` Represents an identity gate of any number of qudits of any size. :param qudits: The number of qudits represented by this identity. :param d: The size of qudits represented by this identity (2 for qubits, 3 for qutrits, etc.) .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: XGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: YGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: ZGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: SXGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: ZXZXZGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: XZXZGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: U3Gate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __eq__(self, other) Return self==value. .. py:method:: __repr__(self) Return repr(self). .. py:class:: U2Gate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __eq__(self, other) Return self==value. .. py:method:: __repr__(self) Return repr(self). .. py:class:: U1Gate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __eq__(self, other) Return self==value. .. py:method:: __repr__(self) Return repr(self). .. py:class:: SingleQutritGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CSUMGate Bases: :py:obj:`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. .. py:attribute:: _csum .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CPIGate Bases: :py:obj:`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. .. py:attribute:: _cpi .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CPIPhaseGate Bases: :py:obj:`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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CNOTGate Bases: :py:obj:`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. .. py:attribute:: _cnot .. py:method:: __eq__(self, other) Return self==value. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CZGate Bases: :py:obj:`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. .. py:attribute:: _gate .. py:method:: __eq__(self, other) Return self==value. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: ISwapGate Bases: :py:obj:`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. .. py:attribute:: _gate .. py:method:: __eq__(self, other) Return self==value. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: XXGate Bases: :py:obj:`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. .. py:attribute:: _gate .. py:method:: __eq__(self, other) Return self==value. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: NonadjacentCNOTGate(qudits, control, target) Bases: :py:obj:`Gate` Represents the two-qubit gate CNOT, but between two qubits that are not necessarily next to each other. :param 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. :param control: The index of the control qubit, relative to the 0th qubit that would be affected by the unitary returned by this gate. :param target: The index of the target qubit, relative to the 0th qubit that would be affected by the unitary returned by this gate. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:method:: validate_structure(self) .. py:class:: UGate(U, d=2, gatename='CUSTOM', gateparams=(), gateindices=None) Bases: :py:obj:`Gate` Represents an arbitrary constant gate, defined by the unitary passed to the initializer. :param U: The unitary for the operation that this gate represents, as a numpy ndarray with datatype="complex128". :param d: The size of qudits for the operation that this gate represents. The default is 2, for qubits. :param gatename: A name for this gate, which will get passed to the Assembler at assembly time. :param gateparams: A tuple of parameters that will get passed to the Assembler at assembly time. :param 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 .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: UpgradedConstantGate(other, df=3) Bases: :py:obj:`Gate` Represents a constant gate, based on the Gate passed to its initializer, but upgraded to act on qudits of a larger size. :param other: A Gate of a lower qudit size. :param df: The final, upgraded qudit size. The default is 3, for upgrading gates from qubits to qutrits. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CUGate(U, gatename='Name', gateparams=(), flipped=False) Bases: :py:obj:`Gate` Represents an arbitrary controlled gate, defined by the unitary passed to the initializer. :param U: The unitary to form the controlled-unitary gate, in the form of a numpy ndarray with dtype="complex128" :param gatename: A name for this controlled gate which will get passed to the Assembler at assembly time. :param gateparams: A tuple of parameters that will get passed to the Assembler at assembly time. :param 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. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: CNOTRootGate Bases: :py:obj:`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. .. py:attribute:: _cnr .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: __repr__(self) Return repr(self). .. py:class:: KroneckerGate(*subgates) Bases: :py:obj:`Gate` Represents the Kronecker product of a list of gates. This is equivalent to performing those gate in parallel in a quantum circuit. :param \*subgates: An sequence of Gates. KroneckerGate will return the kronecker product of the unitaries returned by those Gates. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: appending(self, gate) Returns a new KroneckerGate with the new gate added to the list. :param gate: A Gate to be added to the end of the list of gates in the new KroneckerGate. .. py:method:: _parts(self) .. py:method:: __deepcopy__(self, memo) .. py:method:: __repr__(self) Return repr(self). .. py:method:: validate_structure(self) .. py:class:: ProductGate(*subgates) Bases: :py:obj:`Gate` Represents a matrix product of Gates. This is equivalent to performing those gates sequentially in a quantum circuit. :param subgates: A list of Gates to be multiplied together. ProductGate returns the matrix product of the unitaries returned by those Gates. .. py:method:: matrix(self, v) Generates a matrix using the given vector of input parameters. For a constant gate, v will be empty. :param 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.) :rtype: np.ndarray .. py:method:: mat_jac(self, 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. :param 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. :rtype: tuple .. py:method:: assemble(self, v, i=0) Generates an array of tuples as an intermediate format before being processed by an Assembler for conversion to other circuit formats. :param v: The same numpy array of real floating point numbers that might be passed to matrix(v). :param 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. :rtype: 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. .. py:method:: appending(self, *gates) Returns a new ProductGate with the new gates appended to the end. :param gates: A list of Gates to be appended. .. py:method:: inserting(self, *gates, depth=-1) Returns a new ProductGate with new `gates` inserted at some index `depth`. :param gates: A list of Gates to be inserted. :param 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. .. py:method:: __deepcopy__(self, memo) .. py:method:: __repr__(self) Return repr(self). .. py:method:: validate_structure(self) .. py:class:: Project(path, use_mpi=False) The project class wraps most of the functionality of Qsearch as intended to help manage working with Qsearch. .. py:method:: _save(self) .. py:method:: _checkpoint_path(self, name) .. py:method:: add_compilation(self, name, U, options=None, handle_existing=None, **extraargs) Adds a unitary to be compiled. :param name: A name for this unitary. Must be unique in this Project. :param U: The unitary to be compiled, in the form of a numpy ndarray with dtype="complex128" :param handle_existing: A variable which defines how to behave if a compilation with the given name already exists. If it is set to "ignore", it will simply return without doing anything. If it is set to "overwrite", it will overwrite the previous entry. If it is set to the default of None, it will offer a warning asking the user to remove and re-add the compilation. :param options: The options passed to this function will be used only when this compilation is run. :param extraargs: The extraargs passed to this function will be used only when this compilation is run. .. py:method:: __setitem__(self, keyword, value) .. py:method:: configure_compiler_override(self, keyword, value) An unsafe method that allows the user to set global Project Options even if there is existing work. .. py:method:: __getitem__(self, keyword) .. py:method:: __delitem__(self, keyword) .. py:method:: configure(self, **dictionary) Adds multiple options to the global Project Options at once. .. py:method:: reset(self, name=None) Resets a Project, removing any work done but not the initial configurations. :param name: Optionally specify a particular compilation by name to reset .. py:method:: remove_compilation(self, name) Removes a compilation from a Project. :param name: The name of the compilation to remove .. py:method:: clear(self, name=None) Clears a Project, reverting it to a state similar to a newly created Project. :param name: Optionally specify a particular compilation by name to clear .. py:method:: __enter__(self) .. py:method:: __exit__(self, exc_typ, exc_val, exc_tb) .. py:method:: set_defaults(self, defaults=standard_defaults) Updates the Project Options with the standard defaults from defaults.py, or a provided dictionary. .. py:method:: set_smart_defaults(self, smart_defaults=standard_smart_defaults) Updates the Project Options with the standard smart_defaults from defaults.py, or a provided dictionary .. py:method:: run(self) Runs all of the compilations in the Project. .. py:method:: post_process(self, postprocessor, name=None, options=None, **xtraargs) Post-processes the specified compilation, or all compilations if name is None, using the specified postprocessor. :param postprocessor: The qsearch.post_processing.PostProcessor to run on the compilation or project :param name: Optionally specify a particular compilation by name to reset :param options: Options to pass to the qsearch.post_processing.PostProcessor passed in `postprocessor` :param extraargs: Extra arguments passed as options to the qsearch.post_processing.PostProcessor passed in `postprocessor` .. py:method:: complete(self) Returns a True if all compilations in the Project have finished and False otherwise. .. py:method:: finish(self) Called when done running compilations in order to end MPI tasks. .. py:method:: status(self, name=None, logger=None) Prints a status update on how much of a Project has finished running. :param name: Optionally specify which compilation to check the status of .. py:method:: compilations(self) :property: The list of names corresponding to compilations on this Project. .. py:method:: _compilation_status(self, name) .. py:method:: _overall_status(self) .. py:method:: get_result(self, name) Get the result of a compilation. :param name: The name of the compilation to get the result dictionary from :returns: The result dictionary for a finished compilation. Usually this contains the entries "structure", a Gate, and "parameters", an array of real number parameters. :rtype: dict .. py:method:: get_target(self, name) Get the target unitary of a compilation. :param name: The name of the compilation to get the target from :returns: The target unitary of the compilation :rtype: np.ndarray .. py:method:: get_time(self, name) Get the runtime that it took to run a compilation. :param name: The name of the compilation to get the runtime of :returns: The number of seconds the compilation took :rtype: float .. py:method:: get_options(self, name=None) Get the qsearch.options.Options object from a compilation of project :param name: Optionally pass the name of the compilation to get the qsearch.options.Options object from :returns: the requested options object :rtype: qsearch.options.Options .. py:method:: assemble(self, name, options=None, **xtraargs) Assembles a compilation using the Assembler specified as assembler in the Options. :param name: The compilation to assemble :param options: Contains the qsearch.assemblers.Assembler to use in assembly :returns: The resulting assembled code :rtype: str