QSystem
1.2.0
|
Quantum circuit simulator class. More...
#include <qsystem.h>
Public Member Functions | |
QSystem (size_t num_qbits, size_t seed=42, str representation="bitwise", size_t init=0) | |
Constructor. More... | |
~QSystem () | |
void | evol (char gate, size_t qbit, size_t count=1, bool invert=false) |
Apply a quantum gate. More... | |
void | rot (char axis, double angle, size_t qbit, size_t count=1) |
Rotate a qubit in X, Y or Z axis. More... | |
void | u3 (double theta, double phi, double lambd, size_t qbit, size_t count=1) |
Apply an arbitrary u3 gate. More... | |
void | u2 (double phi, double lambd, size_t qbit, size_t count=1) |
Apply an arbitrary u2 gate. More... | |
void | u1 (double lambd, size_t qbit, size_t count=1) |
Apply an arbitrary u1 (phase) gate. More... | |
void | apply (Gate gate, size_t qbit, size_t count=1, bool invert=false) |
Apply a gate from a Gate class. More... | |
void | cnot (size_t target, vec_size_t control) |
Apply a controlled not. More... | |
void | cphase (complex phase, size_t target, vec_size_t control) |
Apply a controlled phase. More... | |
void | qft (size_t qbit_begin, size_t qbit_end, bool invert=false) |
Apply a quantum Fourier transformation. More... | |
void | swap (size_t qbit_a, size_t qbit_b) |
Swap two qubit. More... | |
void | measure (size_t qbit, size_t count=1) |
Measure qubits in the computational base. More... | |
void | measure_all () |
Measure all qubits in the computational base. More... | |
vec_int | bits () |
Get the measurements results. More... | |
void | flip (char gate, size_t qbit, double p) |
Apply a bit, phase or bit-phase flip error. More... | |
void | amp_damping (size_t qbit, double p) |
Apply an amplitude damping channel error. More... | |
void | dpl_channel (size_t qbit, double p) |
Apply a depolarization channel error. More... | |
void | sum (size_t qbit, vec_str kraus, vec_float p) |
Apply a sum operator. More... | |
str | __str__ () |
Get system representation in a string. More... | |
size_t | size () |
Get the number of qubits. More... | |
str | representation () |
Get the system representation. More... | |
void | save (str path) |
Save the quantum representation in a file. More... | |
void | load (str path) |
Load the quantum representation from a file. More... | |
void | change_to (str new_representation) |
Change the system representation. More... | |
py_obj | get_qbits () |
Get the matrix of the quantum system. More... | |
void | set_qbits (vec_size_t row_ind, vec_size_t col_ptr, vec_complex values, size_t num_qbits, str representation) |
Change the matrix of the quantum system. More... | |
void | add_ancillas (size_t num_qbits, size_t init=0) |
Add ancillary qubits. More... | |
void | rm_ancillas () |
Remove all ancillary qubits. More... | |
void | sync () |
QSystem::QSystem | ( | size_t | num_qbits, |
size_t | seed = 42 , |
||
str | representation = "bitwise" , |
||
size_t | init = 0 |
||
) |
Constructor.
The qubits are initialized in the state \(\left|\text{init}\right>\).
num_qbits | number of qubits in the system. |
seed | for the pseudorandom number generator. |
representation | of the system, use "bitwise" , "vector" or "matrix" (for density matrix). |
init | initial state. |
Definition at line 30 of file qs_utility.cpp.
QSystem::~QSystem | ( | ) |
Definition at line 58 of file qs_utility.cpp.
std::string QSystem::__str__ | ( | ) |
Get system representation in a string.
This method is used in Python to cast a instance to str
.
Definition at line 77 of file qs_utility.cpp.
void QSystem::add_ancillas | ( | size_t | num_qbits, |
size_t | init = 0 |
||
) |
Add ancillary qubits.
The ancillaries qubits are added to the end of the system and can be used in any method.
num_qbits | number of ancillas added. |
init | initial state of the ancillas. |
Definition at line 30 of file qs_ancillas.cpp.
void QSystem::amp_damping | ( | size_t | qbit, |
double | p | ||
) |
Apply an amplitude damping channel error.
Apply the Kraus operator
\[ E_0 = \begin{bmatrix}1&0\\0&\sqrt{1-p}\end{bmatrix}\\ E_1 = \begin{bmatrix}0&0\\ \sqrt{p}&0\end{bmatrix}, \]
The system must be in density matrix representation to use this method, otherwise you can use the flow code to achieve a similar result:
Definition at line 51 of file qs_errors.cpp.
void QSystem::apply | ( | Gate | gate, |
size_t | qbit, | ||
size_t | count = 1 , |
||
bool | invert = false |
||
) |
Apply a gate from a Gate class.
The gate will be applied from the qubits qbit
to qbit+cout*(size of the gate)
.
gate | instace of Gate class |
qbit | qubit affected by the gate. |
count | number of successive repetitions of the gate. |
invert | if true, apply the inverse quantum gate. |
Definition at line 235 of file qs_evol.cpp.
std::vector< int > QSystem::bits | ( | ) |
Get the measurements results.
The measurements results are stored in a list. Where the n-th item is the measurement result of the qubit n. If the n-th qubits has never been measured it's value is None
.
Definition at line 101 of file qs_measure.cpp.
void QSystem::change_to | ( | str | new_representation | ) |
Change the system representation.
The change from vector representation to density matrix is done by \(\left|\psi\right> \rightarrow \left|\psi\right>\mkern-7mu\left<\psi\right|\). It is not possible to change from density matrix representation to any other representation.
new_representation | use "vector" to change vector representation a |
Definition at line 159 of file qs_utility.cpp.
void QSystem::cnot | ( | size_t | target, |
vec_size_t | control | ||
) |
Apply a controlled not.
Apply a not in the target
qubit if all the control
qubits are in the representation \(\left|1\right>\).
target | target qubit. |
control | list of control qubits. |
Definition at line 267 of file qs_evol.cpp.
void QSystem::cphase | ( | complex | phase, |
size_t | target, | ||
vec_size_t | control | ||
) |
Apply a controlled phase.
Apply \(\begin{bmatrix}1&0\\0&e^\phi\end{bmatrix}\), where \(e^\phi\) = phase
, in the target
qubit if all the control
qubits are in the representation \(\left|1\right>\).
phase | \(e^\phi\) value. |
target | target qubit. |
control | list of control qubits. |
Definition at line 293 of file qs_evol.cpp.
void QSystem::dpl_channel | ( | size_t | qbit, |
double | p | ||
) |
Apply a depolarization channel error.
Apply the operator
\[ \mathcal{E}(\rho) = \left(1-{3p\over4}\right)\rho +{p\over4}(X\rho X+Y\rho Y+Z\rho Z), \]
that takes the qubit to the maximally mixed representation with probability p
.
The system must be in density matrix representation to use this method.
qbit | qubit effected by the error. |
p | probability of the error occur. |
Definition at line 66 of file qs_errors.cpp.
void QSystem::evol | ( | char | gate, |
size_t | qbit, | ||
size_t | count = 1 , |
||
bool | invert = false |
||
) |
Apply a quantum gate.
The gate will be applied from the qubits qbit
to qbit+cout
. The follow gates are available:
'I'
= \(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\)'Y'
= \(\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\)'X'
= \(\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\)'Z'
= \(\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\)'H'
= \({1\over\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\)'S'
= \(\begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}\)'T'
= \(\begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi\over4} \end{bmatrix}\).gate | name of the gate that will be user. |
qbit | qubit affected by the gate. |
count | number of successive repetitions of the gate. |
invert | if true, apply the inverse quantum gate. |
Definition at line 32 of file qs_evol.cpp.
void QSystem::flip | ( | char | gate, |
size_t | qbit, | ||
double | p | ||
) |
Apply a bit, phase or bit-phase flip error.
Apply the Kraus operator
\[ E_0 = \sqrt{p}\sigma\\ E_1 = \sqrt{1-p}I, \]
where \(\sigma\) is \(\begin{bmatrix}1&0\\0&1\end{bmatrix}\) if gate
is 'X'
, \(\begin{bmatrix}1&0\\0&1\end{bmatrix}\) if gate
is 'Z'
or \(\begin{bmatrix}1&0\\0&-1\end{bmatrix}\) if gate
is 'Y'
.
gate | use 'X' for bit flip, 'Z' for phase flip or 'Y' for bit-phase flip. |
qbit | qubit effected by the error. |
p | probability of the error occur. |
Definition at line 30 of file qs_errors.cpp.
PyObject * QSystem::get_qbits | ( | ) |
Get the matrix of the quantum system.
This method is used in Python by the non-member function get_matrix
.
Definition at line 108 of file qs_utility.cpp.
void QSystem::load | ( | str | path | ) |
Load the quantum representation from a file.
When load, all qubits are set to non-ancillary.
path | to the file that will be loaded. |
Definition at line 217 of file qs_utility.cpp.
void QSystem::measure | ( | size_t | qbit, |
size_t | count = 1 |
||
) |
Measure qubits in the computational base.
Measure qubits from qbit
to qbit*cout
. All the measurements results are assessable throw the QSystem::bits method.
qbit | qubit affected by the measurement. |
count | number qubits measured from qbit . |
Definition at line 30 of file qs_measure.cpp.
void QSystem::measure_all | ( | ) |
Measure all qubits in the computational base.
The measurements results are assessable throw the QSystem::bits method.
Definition at line 96 of file qs_measure.cpp.
void QSystem::qft | ( | size_t | qbit_begin, |
size_t | qbit_end, | ||
bool | invert = false |
||
) |
Apply a quantum Fourier transformation.
Apply the QFT in the range of qubits (qbit_begin
, qbit_end
].
qbit_begin | first qubit affected. |
qbit_end | last qubit affected +1. |
invert | if true, apply the inverse quantum gate. |
Definition at line 347 of file qs_evol.cpp.
std::string QSystem::representation | ( | ) |
Get the system representation.
"bitwise"
, "vector"
or "matrix"
(for density matrix).Definition at line 196 of file qs_utility.cpp.
void QSystem::rm_ancillas | ( | ) |
Remove all ancillary qubits.
If the state is in vector or bitwise representation the ancillas are measured before been removed. If the state is in density matrix representation, the ancillas are removed by a partial trace operation, without been measured.
Definition at line 63 of file qs_ancillas.cpp.
void QSystem::rot | ( | char | axis, |
double | angle, | ||
size_t | qbit, | ||
size_t | count = 1 |
||
) |
Rotate a qubit in X, Y or Z axis.
The rotation will be applied from the qubits qbit
to qbit+cout
The rotation matrix for each axis are:
'X'
= \(\begin{bmatrix} \cos{\theta\over2} & -i\sin{\theta\over2} \\ -i\sin{\theta\over2} & \cos{\theta\over2} \\ \end{bmatrix}\)'Y'
= \(\begin{bmatrix} \cos{\theta\over2} & -\sin{\theta\over2} \\ \sin{\theta\over2} & \cos{\theta\over2} \\ \end{bmatrix}\)'Z'
= \(\begin{bmatrix} -e^{i{\theta\over2}} & 0 \\ 0 & e^{i{\theta\over2}} \\ \end{bmatrix}\)axis | of the rotation |
angle | \(\theta\) of the rotation |
qbit | qubit affected by the rotation. |
count | number of successive repetitions of the gate. |
Definition at line 64 of file qs_evol.cpp.
void QSystem::save | ( | str | path | ) |
Save the quantum representation in a file.
The file is in a machine dependent binary format defined by the library Armadillo.
path | to the file that will be created. |
Definition at line 201 of file qs_utility.cpp.
void QSystem::set_qbits | ( | vec_size_t | row_ind, |
vec_size_t | col_ptr, | ||
vec_complex | values, | ||
size_t | num_qbits, | ||
str | representation | ||
) |
Change the matrix of the quantum system.
This method is used in Python by the non-member function set_matrix
.
row_ind | row indices. |
col_ptr | column pointers. |
values | non-zero values. |
num_qbits | number of qubits. |
representation | representation. |
Definition at line 142 of file qs_utility.cpp.
size_t QSystem::size | ( | ) |
Get the number of qubits.
The ancillary qubits are include in the count.
Definition at line 103 of file qs_utility.cpp.
void QSystem::sum | ( | size_t | qbit, |
vec_str | kraus, | ||
vec_float | p | ||
) |
Apply a sum operator.
To apply some Kraus operator like
\[ E_1 = {\sqrt{p_1}} (U_{11}\otimes\dots\otimes U_{1n})\\ E_2 = {\sqrt{p_2}} (U_{21}\otimes\dots\otimes U_{2n})\\ \vdots\\ E_m = {\sqrt{p_m}} (U_{m1}\otimes\dots\otimes U_{mn}), \]
pass the follow parameters
kraus
= [ \(U_{11}\otimes\dots\otimes U_{1n},\, U_{21}\otimes\dots\otimes U_{2n},\, \dots,\, U_{m1}\otimes\dots\otimes U_{mn}\)] andp
= [ \(p_1,\,p_2,\,\dots,\,p_m\)]The system must be in density matrix representation to use this method, otherwise you can use the flow code to achieve a similar result:
qbit | first qubit effected by the error. |
kraus | Kraus operators list. |
p | probability list. |
Definition at line 81 of file qs_errors.cpp.
void QSystem::swap | ( | size_t | qbit_a, |
size_t | qbit_b | ||
) |
Swap two qubit.
qbit_a | qubit that gonna be swapped with qbit_b . |
qbit_b | qubit that gonna be swapped with qbit_a . |
Definition at line 321 of file qs_evol.cpp.
void QSystem::sync | ( | ) |
Definition at line 379 of file qs_evol.cpp.
void QSystem::u1 | ( | double | lambd, |
size_t | qbit, | ||
size_t | count = 1 |
||
) |
Apply an arbitrary u1 (phase) gate.
The gate will be applied from the qubits qbit
to qbit+cout
.
\[ u1 = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\lambda} \end{bmatrix} \]
lambd | = \(\lambda\) |
qbit | qubit affected by the gate. |
count | number of successive repetitions of the gate. |
Definition at line 214 of file qs_evol.cpp.
void QSystem::u2 | ( | double | phi, |
double | lambd, | ||
size_t | qbit, | ||
size_t | count = 1 |
||
) |
Apply an arbitrary u2 gate.
The gate will be applied from the qubits qbit
to qbit+cout
.
\[ u2 = {1\over\sqrt{2}} \begin{bmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\lambda+\phi)} \end{bmatrix} \]
phi | = \(\phi\) |
lambd | = \(\lambda\) |
qbit | qubit affected by the gate. |
count | number of successive repetitions of the gate. |
Definition at line 170 of file qs_evol.cpp.
void QSystem::u3 | ( | double | theta, |
double | phi, | ||
double | lambd, | ||
size_t | qbit, | ||
size_t | count = 1 |
||
) |
Apply an arbitrary u3 gate.
The gate will be applied from the qubits qbit
to qbit+cout
.
\[ u3 = \begin{bmatrix} \cos{\theta\over2} & -e^{i\lambda}\sin{\theta\over2} \\ e^{i\phi}\sin{\theta\over2} & e^{i(\lambda+\phi)}\cos{\theta\over2} \end{bmatrix} \]
theta | = \(\theta\) |
phi | = \(\phi\) |
lambd | = \(\lambda\) |
qbit | qubit affected by the gate. |
count | number of successive repetitions of the gate. |
Definition at line 125 of file qs_evol.cpp.