Circuit
Circuit class used for the simulations.
The circuit is defined as a list of layers, and each layer is a list of Instructions, i.e. a list [QCOperation, sites]. This structure ensures a simplified analysis of which operations could be executed in parallel. Furthermore, we point out a couple of further important elements of the class:
- qregisters: the quantum registers are a way of keeping track of
“temporary” qubits, that can be added and removed at will. With the class methods
add_qregister()andremove_qregister()it is possible to apply such operations. Then, when you apply an operation you can always specify to which register you are referring, without having to worry about the indexing. In particular, qubits in the same register are naturally ordered from left to right. So, if you want to apply a not gate to the left-most qubit of the register a1 you will have to call qcirc.x(0, ‘a1’), since the left-most has index 0 for the register ‘a1’;
- cregisters: the classical registers, where you store the information of
projective mesurements happened previously on the system and their probability. So, the position idx of the cregister will hold the tuple
(meas_result, meas_prob. Each operation can be conditioned on the value of a classical register. By calling the methodinspect_cregister(creg, idx)()where creg is the name of your classical register, you can have access to the value of the bit at the index idx. If idx=None instead, the full register is treated as the binary representation of a decimal number, and that decimal number is given by the function. Remember to add the classical registers you need beforehand, usingadd_cregister(): the quantum circuit start without any of them!
To add operations to the circuit there are three main ways:
the
add(), to append the operation at the end of the circuit- some methods to apply widely-used operations to the circuit with a simplified
notation.
- the
insert()method, to insert an operation at any given layer of the circuit. However, this method is dangerous and is not recommended.
- the
The str_repr() is meant to give a way to visualize the circuit, but is not needed
for the functioning of the class, and it is still under development.
- class qmatchatea.circuit.circuit.Qcircuit(num_sites, local_dim=2)[source]
Class used to represent a quantum circuit in the qmatchatea library. Each layer of the circuit is a list containing instructions, i.e. lists [QCOperation, sites].
When iterating over the class the iterators returns the layers.
Iterating over the layer returns the instructions, with all permutations of indexes already taken care of, based on the quantum registers.
This means that, once you obtain the operation, you simply need to apply it on the defined sites of the mps, without further complexity.
However, you should not run it by yourself, it is preferred to use the
QcMps.run_from_qcirc().Parameters
- num_sites: int
Number of sites in the circuit
- local_dim: int, optional
Local dimension of the single site. Default to 2.
- add(operation, sites, register='default')[source]
Add an operation to the circuit
Parameters
- operation
QCOperation Operation to be added to the
- sitesinteger of array-like of integers
Qubits to which the operation is applied
- registerstr or array of str
Register to which the operations are applied. Default are both to default.
- operation
- add_cregister(name, num_bits)[source]
Add a classical register with name name and length num_bits
Parameters
- namestr
Name of the classical register
- num_bitsint
Number of bits the register can store
- add_qregister(new_register, positions, reference_register='default')[source]
Add a new register to the circuit, in a specific positions. The positions is the index of the link where the site will be added, so 0 is attached to the left, num_sites is attached to the right. An intermediate number attaches it in the middle. This link number is taken with respect to the reference register.
This function is a simple loop over add_site, but we expect it to be a very useful function in runtime
Parameters
- new_registerstr
Name of the register that will track the index of the new site
- positionsarray-like of ints
Index of the link where the new site should be added in the register
- reference_register: str
Register with respect to which the position is taken. Default to ‘default’.
- add_site(new_register, position, reference_register='default')[source]
Add a site to the circuit, in a specific position. The position is the index of the link where the site will be added, so 0 is attached to the left, num_sites is attached to the right. An intermediate number attaches it in the middle.
Parameters
- new_registerstr
Name of the register that will track the index of the new site
- positionint
Index of the link where the new site should be added in the register
- reference_register: str
Register with respect to which the position is taken. Default to ‘default’.
- cp(theta, pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a controlled phase gate in the position pos in the qregisters qreg. pos should be an array, qreg should be an array if the qubits are in different qregisters
Parameters
- thetafloat
Rotation angle for controlled phase gate
- poslist of two ints
Index, relative to the quantum register, where you want to apply the operation
- qreglist of str, str or None, optional
Quantum registers where you want to apply the operation. If only a str is provided, the program assumes you want to apply the gate inside the same quantum register. If a list of two strings is provided, the gate is applied between different quantum registers. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’.
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- property cregisters
Return the full list of classical registers
- cx(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a controlled not gate in the position pos in the qregisters qreg. pos should be an array, qreg should be an array if the qubits are in different qregisters
Parameters
- poslist of two ints
Index, relative to the quantum register, where you want to apply the operation
- qreglist of str, str or None, optional
Quantum registers where you want to apply the operation. If only a str is provided, the program assumes you want to apply the gate inside the same quantum register. If a list of two strings is provided, the gate is applied between different quantum registers. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’.
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- cz(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a controlled z gate in the position pos in the qregisters qreg. pos should be an array, qreg should be an array if the qubits are in different qregisters
Parameters
- poslist of two ints
Index, relative to the quantum register, where you want to apply the operation
- qreglist of str, str or None, optional
Quantum registers where you want to apply the operation. If only a str is provided, the program assumes you want to apply the gate inside the same quantum register. If a list of two strings is provided, the gate is applied between different quantum registers. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’.
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- fill_qcoperators(operators)[source]
Fill the QCoperators dictionary with all the operators in the Qcircuit
Parameters
- operators:
QCOperators operators class to fill
- operators:
- classmethod from_qiskit(qc)[source]
Initialize the Qcircuit class from a qiskit quantum circuit
Parameters
- qc
QuantumCircuit qiskit QuantumCircuit that will initialize the Qcircuit
- qc
- gate(gate, pos, name='op', qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a matrix to the circuit. The dimensions of the matrix should be consistent with the number of qubits to which it should be applied
Parameters
- gatenp.ndarray
gate matrix
- posint or array-like of ints
Indexes of the sites to which it should be applied
- namestr, optional
Unique identifier of the gate. Default to ‘op’
- qregstr or array-like of str, optional
qregister to which the gate should be applied
- h(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a hadamard gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- insert(operation, sites, layer, register='default', inlayer=True)[source]
Insert an operation after a specific layer on the sites If inlayer is True simply add it into the layer without checking if an operation is already there, otherwise check for the operation and insert a new layer
Parameters
- operation
QCOperation Operation to be added to the
- sitesint of array-like of integers
Qubits to which the operation is applied
- layer: int
integer identifying the layer after which we want to insert the operation
- registerstr or array of str
Register to which the operations are applied. Default are both to default.
- inlayer: bool, optional
If False, and the position in the layer is already occupied, add a new layer. Otherwise add it to that layer nevertheless. Default to True.
- operation
- inspect_cregister(register, idx=None)[source]
Retrieve the decimal value of the binary number stored in the classical register register if idx=None, otherwise it returns the value of the bit at position idx
Parameters
- registerstr
Name of the classical register
- idxint, optional
Index of the bit you want to recover. If None, you will recover the decimal representation of the full register. Default to None.
- property local_dim
Local dimension property
- measure_observables(name, observables, operators, qreg=None, c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Perform a measurement step of all the observables defined in the observable parameter.
Parameters
- namestr
Name of the observables step
- observables:
TNObservables Observables to be measured at this step
- operators:
QCOperators Class containing the operators present in the Observables
- qregstr or None, optional
Quantum register where the observables should be measured. If None, they are measured on the FULL circuit, regardless of the registers
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation
- measure_projective(pos, cl_idx, qreg='default', creg='default', selected_output=None, c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a projective measurement in the position pos in the qregister qreg. You can choose the output of the measure. The result is stored in the classical qregister creg at index cl_idx
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- cl_idxint
Index of the classical register where to store the result of the measurement
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- cregisterstr, optional
Classical register where the result of the measurement is stored. Default to ‘default’
- selected_outputint, optional
Output selected a priori. Default to None.
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- modify_cregister(value, name, idx)[source]
Modify the value of a measured bit in the classical register
Parameters
- valuetuple of (int, float)
Value of the measured qubit to be saved in the classical register and of the probability of that outcome
- namestr
Name of the classical register
- idxint
Index of the bit where to save value
- property num_classical_sites
Number of classical sites, i.e. bits
- property num_layers
Number of layers in the circuit
- property num_qubits
Number of sites property, with another name for simplicity of interfaces
- property num_sites
Number of sites property
- p(theta, pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a Phase gate in the position pos in the qregister qreg with a phase theta
Parameters
- thetafloat
Rotation angle for the phase gate
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- remove_qregister(register)[source]
Remove a register from the circuit.
Parameters
- registerstr
Name of the register to be removed
- remove_site(register, position)[source]
Remove a site from the circuit. The site to be removed is identified by its register and the position in the register.
Parameters
- registerstr
Name of the register where the site to remove is
- positionint
Index of the site to remove
- renormalize(c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Renormalize the state
Parameters
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation
- c_if
- s(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a S gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- sadg(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply the adjoint of an S gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- swap(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a swap gate in the position pos in the qregisters qreg. pos should be an array, qreg should be an array if the qubits are in different qregisters
Parameters
- poslist of two ints
Index, relative to the quantum register, where you want to apply the operation
- qreglist of str, str or None, optional
Quantum registers where you want to apply the operation. If only a str is provided, the program assumes you want to apply the gate inside the same quantum register. If a list of two strings is provided, the gate is applied between different quantum registers. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’.
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- t(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a T gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- tadg(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply the adjoint of an T gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- to_matrix(sparse=False, max_qubit_equivalent=20, qiskit_order=True)[source]
Return the matrix representation of the circuit. It is a unitary matrix if no noise/non-unitary matrix has been applied. Notice that this computation scales as \(d^{2n}\), where \(d\) is the local dimension of the single degree of freedom and \(n\) the number of sites. Furthermore, the function is not optimized and might take long.
Parameters
- sparsebool, optional
If True, return a csr sparse matrix instead of a numpy array. Default to False.
- max_qubit_equivalent: int, optional
Maximum number of qubit sites the MPS can have and still be transformed into a matrix. If the number of sites is greater, it will throw an exception. Default to 20.
- qiskit_orderbool, optional
If True, return the matrix using qiskit ordering. Default to True.
Returns
- matrixnp.ndarray or sp.csr_matrix
Numpy array or sparse matrix representing the system unitary
- write(dest, id_mapping)[source]
Write on file (originally written for the Fortran simulator), with the correct formatting
Parameters
- dest: str or filehandle
If str, write on that file. Otherwise, write using that filehandle
- id_mapping: dict
Mapping from the operator name to the operator id
- x(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a x gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- y(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a y gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.
- z(pos, qreg='default', c_if=<qmatchatea.circuit.operations.ClassicalCondition object>)[source]
Apply a z gate in the position pos in the qregister qreg
Parameters
- posint
Index, relative to the quantum register, where you want to apply the operation
- qregstr or None, optional
Quantum register where you want to apply the operation. If None, the “raw index” is considered, i.e. without taking into account the quantum register reindexing. Default to ‘default’
- c_if
ClassicalCondition, optional Classical condition to be checked for applying the operation on the simulation.