QSystem  1.2.0
Classes | Public Member Functions | List of all members
QSystem Class Reference

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 ()
 

Detailed Description

Quantum circuit simulator class.

Definition at line 31 of file qsystem.h.

Constructor & Destructor Documentation

◆ QSystem()

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>\).

Parameters
num_qbitsnumber of qubits in the system.
seedfor the pseudorandom number generator.
representationof the system, use "bitwise", "vector" or "matrix" (for density matrix).
initinitial state.

Definition at line 30 of file qs_utility.cpp.

◆ ~QSystem()

QSystem::~QSystem ( )

Definition at line 58 of file qs_utility.cpp.

Member Function Documentation

◆ __str__()

std::string QSystem::__str__ ( )

Get system representation in a string.

This method is used in Python to cast a instance to str.

Returns
String with the system representation.
See also
QSystem::size QSystem::representation

Definition at line 77 of file qs_utility.cpp.

◆ add_ancillas()

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.

Parameters
num_qbitsnumber of ancillas added.
initinitial state of the ancillas.
See also
QSystem::rm_ancillas

Definition at line 30 of file qs_ancillas.cpp.

◆ amp_damping()

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:

def amp_damping(q, qbit, p):
from random import choices
if choices([True, False], weights=[p, 1-p])[0]:
q.measure(qbit)
if q.bits[qbit] == 1:
q.evol('X', qbit)
See also
QSystem::flip QSystem::dpl_channel QSystem::sum

Definition at line 51 of file qs_errors.cpp.

◆ apply()

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).

Parameters
gateinstace of Gate class
qbitqubit affected by the gate.
countnumber of successive repetitions of the gate.
invertif true, apply the inverse quantum gate.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft QSystem::swap

Definition at line 235 of file qs_evol.cpp.

◆ bits()

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.

Returns
List of the measurement result.
See also
QSystem::measure QSystem::measure_all

Definition at line 101 of file qs_measure.cpp.

◆ change_to()

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.

Parameters
new_representationuse "vector" to change vector representation a
See also
QSystem::representation

Definition at line 159 of file qs_utility.cpp.

◆ cnot()

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>\).

Parameters
targettarget qubit.
controllist of control qubits.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u2 QSystem::u1 QSystem::cphase QSystem::qft QSystem::swap

Definition at line 267 of file qs_evol.cpp.

◆ cphase()

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>\).

Parameters
phase\(e^\phi\) value.
targettarget qubit.
controllist of control qubits.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::qft QSystem::swap

Definition at line 293 of file qs_evol.cpp.

◆ dpl_channel()

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.

Parameters
qbitqubit effected by the error.
pprobability of the error occur.
See also
QSystem::flip QSystem::amp_damping QSystem::sum

Definition at line 66 of file qs_errors.cpp.

◆ evol()

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}\).
Parameters
gatename of the gate that will be user.
qbitqubit affected by the gate.
countnumber of successive repetitions of the gate.
invertif true, apply the inverse quantum gate.
See also
QSystem::rot QSystem::u3 QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft QSystem::swap

Definition at line 32 of file qs_evol.cpp.

◆ flip()

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'.

Parameters
gateuse 'X' for bit flip, 'Z' for phase flip or 'Y' for bit-phase flip.
qbitqubit effected by the error.
pprobability of the error occur.
See also
QSystem::amp_damping QSystem::dpl_channel QSystem::sum

Definition at line 30 of file qs_errors.cpp.

◆ get_qbits()

PyObject * QSystem::get_qbits ( )

Get the matrix of the quantum system.

This method is used in Python by the non-member function get_matrix.

def get_matrix(q):
from scipy import sparse
return sparse.csc_matrix(q.get_qbits()[0], q.get_qbits()[1])
Returns
Tuple used to initialize a scipy space matrix.
See also
QSystem::set_qbits

Definition at line 108 of file qs_utility.cpp.

◆ load()

void QSystem::load ( str  path)

Load the quantum representation from a file.

When load, all qubits are set to non-ancillary.

Parameters
pathto the file that will be loaded.
See also
QSystem::save

Definition at line 217 of file qs_utility.cpp.

◆ measure()

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.

Parameters
qbitqubit affected by the measurement.
countnumber qubits measured from qbit.
See also
QSystem::measure_all QSystem::bits

Definition at line 30 of file qs_measure.cpp.

◆ measure_all()

void QSystem::measure_all ( )

Measure all qubits in the computational base.

The measurements results are assessable throw the QSystem::bits method.

See also
QSystem::measure QSystem::bits

Definition at line 96 of file qs_measure.cpp.

◆ qft()

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].

Parameters
qbit_beginfirst qubit affected.
qbit_endlast qubit affected +1.
invertif true, apply the inverse quantum gate.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::swap

Definition at line 347 of file qs_evol.cpp.

◆ representation()

std::string QSystem::representation ( )

Get the system representation.

Returns
"bitwise", "vector" or "matrix" (for density matrix).
See also
QSystem::size QSystem::change_to

Definition at line 196 of file qs_utility.cpp.

◆ rm_ancillas()

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.

See also
QSystem::rm_ancillas

Definition at line 63 of file qs_ancillas.cpp.

◆ rot()

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}\)
Parameters
axisof the rotation
angle\(\theta\) of the rotation
qbitqubit affected by the rotation.
countnumber of successive repetitions of the gate.
See also
QSystem::evol QSystem::u3 QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft QSystem::swap

Definition at line 64 of file qs_evol.cpp.

◆ save()

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.

Parameters
pathto the file that will be created.
See also
QSystem::load

Definition at line 201 of file qs_utility.cpp.

◆ set_qbits()

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.

def set_matrix(q, m):
from scipy import sparse
from math import log2
m = sparse.csc_matrix(m)
if m.shape[0] == m.shape[1]:
representation = 'matrix'
else:
representation = 'vector'
size = int(log2(m.shape[0]))
q.set_qbits(m.indices.tolist(), m.indptr.tolist(), m.data.tolist(), size, representation)
Parameters
row_indrow indices.
col_ptrcolumn pointers.
valuesnon-zero values.
num_qbitsnumber of qubits.
representationrepresentation.
See also
QSystem::get_qbits

Definition at line 142 of file qs_utility.cpp.

◆ size()

size_t QSystem::size ( )

Get the number of qubits.

The ancillary qubits are include in the count.

Returns
Number of qubits in the system.
See also
QSystem::representation

Definition at line 103 of file qs_utility.cpp.

◆ sum()

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}\)] and
  • p = [ \(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:

def sum(q, qbit, kraus, p):
from random import choices
aux = 0
for gate in choices(kraus, weights=p)[0]:
q.evol(gate, qbit+aux)
aux += 1
Parameters
qbitfirst qubit effected by the error.
krausKraus operators list.
pprobability list.
See also
QSystem::flip QSystem::amp_damping QSystem::dpl_channel

Definition at line 81 of file qs_errors.cpp.

◆ swap()

void QSystem::swap ( size_t  qbit_a,
size_t  qbit_b 
)

Swap two qubit.

Parameters
qbit_aqubit that gonna be swapped with qbit_b.
qbit_bqubit that gonna be swapped with qbit_a.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft

Definition at line 321 of file qs_evol.cpp.

◆ sync()

void QSystem::sync ( )

Definition at line 379 of file qs_evol.cpp.

◆ u1()

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} \]

Parameters
lambd= \(\lambda\)
qbitqubit affected by the gate.
countnumber of successive repetitions of the gate.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u2 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft QSystem::swap

Definition at line 214 of file qs_evol.cpp.

◆ u2()

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} \]

Parameters
phi= \(\phi\)
lambd= \(\lambda\)
qbitqubit affected by the gate.
countnumber of successive repetitions of the gate.
See also
QSystem::evol QSystem::rot QSystem::u3 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft QSystem::swap

Definition at line 170 of file qs_evol.cpp.

◆ u3()

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} \]

Parameters
theta= \(\theta\)
phi= \(\phi\)
lambd= \(\lambda\)
qbitqubit affected by the gate.
countnumber of successive repetitions of the gate.
See also
QSystem::evol QSystem::rot QSystem::u2 QSystem::u1 QSystem::apply QSystem::cnot QSystem::cphase QSystem::qft QSystem::swap

Definition at line 125 of file qs_evol.cpp.


The documentation for this class was generated from the following files: