Run circuit

This module provides a simple quantum circuit simulator.

run_circuit.digit(n, k, base=10)[source]

Computes the digit k for the integer n in its representation in base base.

Example:
>>> digit(152, 1)
5
>>> digit(152, 0)
2
Parameters:
  • n (int) – The integer for which the digit is needed.
  • k (int) – The number of the digit.
  • base (int) – The base used for the representation of n.
Returns:

int – The k \(^{th}\) digit of n in base base.

run_circuit.int_name(num)[source]

Converts a number to a string composed of the list of its digits in English.

Example:
>>> int_name(152)
'onefivetwo'
Parameters:num (int) – Number to be converted to a list of digits.
Returns:str – List of digits of num in base 10 concatenated.
run_circuit.kronecker(a, b)[source]

Computes the Kronecker product of a and b.

Example:
>>> a = matrix([[1,0],[0,1]])
>>> b = matrix([[1,2],[3,4]])
>>> kronecker(a,b)
[1 2 0 0]
[3 4 0 0]
[0 0 1 2]
[0 0 3 4]
Parameters:a,b (matrix,vector) – Operands for the kronecker operator.
Returns:matrix or vector – The kronecker product of a and b (return type is the same a type of a).
run_circuit.kronecker_power(a, n)[source]

Computes the n \(^{th}\) Kronecker power of a.

Example:
>>> a = matrix([[1,0],[0,2]])
>>> kronecker_power(a,2)
[1 0 0 0]
[0 2 0 0]
[0 0 2 0]
[0 0 0 4]
Parameters:
  • a (matrix,vector) – The matrix (or vector) to be elevated to the n \(^{th}\) power.
  • n (int) – The power a has to be elevated to.
Returns:

matrix or vector – The n \(^{th}\) kronecker power of a (return type is the same a type of a).

run_circuit.layers_to_matrix(layers)[source]

layers is the same as matrix_layers but with matrix names embedded in them

Example:
>>> I2 = matrix.identity(2)
>>> I4 = matrix.identity(4)
>>> H = matrix([[1,1],[1,-1]])/sqrt(2)
>>> swap = matrix([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])
>>> layers = [[('I',I4),('H',H)],[('H',H),('H',H),('I',I2)],[('I',I2),('S',swap)]]
>>> layers_to_matrix(layers)
[[I4,H],[H,H,I2],[I2,swap]]
Parameters:layers (list[list[(str,matrix)]]) – Circuit under the format described above.
Returns:list[List[(str,int)]] – Circuit under the ready-to-run format described above.
run_circuit.layers_to_printable(layers)[source]

layers is the same as matrix_layers but with matrix names embedded in them

Example:
>>> I2 = matrix.identity(2)
>>> I4 = matrix.identity(4)
>>> H = matrix([[1,1],[1,-1]])/sqrt(2)
>>> swap = matrix([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])
>>> layers = [[('I',I4),('H',H)],[('H',H),('H',H),('I',I2)],[('I',I2),('S',swap)]]
[[('I',2),('H',1)],[('H',1),('H',1),('I',1)],[('I',1),('S',2)]]
Parameters:layers (list[list[(str,matrix)]]) – Circuit under the format described above.
Returns:list[List[(str,int)]] – Circuit under the ready-to-print format described above.
run_circuit.output_commant(command_name, command, output=False, output_to_file=False, w_file=None)[source]

This function is used to print useful informations in various ways, see arguments details for more information.

Example:
>>> output_commant("test",vector(SR,[1,0,0,1]), output=True)
\newcommand{\test}{1 \ket{00} + 1 \ket{11}}
Parameters:
  • command_name (str) – The command name.
  • command (any) – The command content (if type is str, will be printed as such; if vector, vector_to_ket will be called on it and if matrix, latex method from SageMath will be called on it).
  • output (bool) – disables or enables the output.
  • output_to_file (bool) – Whether the output should be in the standard output or in a file.
  • w_file (file) – The opened file to write the output to.
Returns:

None

run_circuit.print_circuit(name_layers, to_latex=False)[source]

Each name is a tuple with the name of the gate and its dimension

Example:
>>> circuit = [[('I',2),('H',1)],[('H',1),('H',1),('I',1)],[('I',1),('S',2)]]
>>> print_circuit(circuit, to_latex=False)
---H---
---H-S-
-H---|-
>>> print_circuit(circuit, to_latex=True)
\begin{align*}
 \Qcircuit @C=1em @R=.7em {
  & \qw       & \gate{H}  & \qw               & \qw\\
  & \qw       & \gate{H}  & \multigate{1}{S}  & \qw\\
  & \gate{H}  & \qw       & \ghost{S}         & \qw
 }
\end{align*}

@multi-gost_, @multi-source_ and @multi-size_ are reserved names, they should not be used as a gate name.

Parameters:
  • name_layers (list[List[(str,int)]]) – Circuit name formalism in the shape of the example given above. First layer should not be empty. sum([item[1] for item in layer]) should be constant for layer in layers
  • latex (bool) – If true, a string is returned containing the circuit in the format given by the LaTeX package qcircuit. Otherwise, the string returned is under a custom format meant to be easily readable in the terminal.
Returns:

str – The circuit in the format described above.

run_circuit.run(matrix_layers, V_init, output=False, output_file=False, file=None, vector_name='V', matrix_name='M')[source]

Runs the algorithm specified by the matrix_layers.

Example:
>>> I2 = matrix.identity(2)
>>> I4 = matrix.identity(4)
>>> H = matrix([[1,1],[1,-1]])/sqrt(2)
>>> swap = matrix([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])
>>> v = vector([1, 0, 0, 0, 0, 0, 0, 0])
>>> layers = [[I4,H],[H,H,I2],[I2,swap]]
>>> run(layers,v)
([(1, 0, 0, 0, 0, 0, 0, 0),
  (1/2*sqrt(2), 1/2*sqrt(2), 0, 0, 0, 0, 0, 0),
  (1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2)),
  (1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2), 1/4*sqrt(2))],
 [
[ 1/2*sqrt(2)  1/2*sqrt(2)            0            0            0            0            0            0]
[ 1/2*sqrt(2) -1/2*sqrt(2)            0            0            0            0            0            0]
[           0            0  1/2*sqrt(2)  1/2*sqrt(2)            0            0            0            0]
[           0            0  1/2*sqrt(2) -1/2*sqrt(2)            0            0            0            0]
[           0            0            0            0  1/2*sqrt(2)  1/2*sqrt(2)            0            0]
[           0            0            0            0  1/2*sqrt(2) -1/2*sqrt(2)            0            0]
[           0            0            0            0            0            0  1/2*sqrt(2)  1/2*sqrt(2)]
[           0            0            0            0            0            0  1/2*sqrt(2) -1/2*sqrt(2)],
[ 1/2    0  1/2    0  1/2    0  1/2    0]  [1 0 0 0 0 0 0 0]
[   0  1/2    0  1/2    0  1/2    0  1/2]  [0 0 1 0 0 0 0 0]
[ 1/2    0 -1/2    0  1/2    0 -1/2    0]  [0 1 0 0 0 0 0 0]
[   0  1/2    0 -1/2    0  1/2    0 -1/2]  [0 0 0 1 0 0 0 0]
[ 1/2    0  1/2    0 -1/2    0 -1/2    0]  [0 0 0 0 1 0 0 0]
[   0  1/2    0  1/2    0 -1/2    0 -1/2]  [0 0 0 0 0 0 1 0]
[ 1/2    0 -1/2    0 -1/2    0  1/2    0]  [0 0 0 0 0 1 0 0]
[   0  1/2    0 -1/2    0 -1/2    0  1/2], [0 0 0 0 0 0 0 1]
])
Parameters:
  • matrix_layers (array[array[matrix]]) – Algorithm described as it would be in a circuit.
  • V_init (vector) – Initial input of the algorithm.
  • output (bool) – If true, outputs are enabled.
  • output_to_file (bool) – If true, trace is returned in file, else it is printed.
  • file (file) – File to output the commands to.
  • vector_name (str) – Base name used for the vectors commands.
  • matrix_name (str) – Base name used for the matrices commands.
Returns:

vector – The states along the execution of the algorithm as well as the matrix corresponding to each layer.