2 * Introduction to quantum_computing::
3 * Functions and Variables for Quantum_Computing::
6 @node Introduction to quantum_computing, Functions and Variables for Quantum_Computing, Package quantum_computing, Package quantum_computing
7 @section Package quantum_computing
9 The @code{quantum_computing} package provides several functions to
10 simulate quantum computing circuits. The state of a system of @var{n}
11 qubits is represented by a list of 2^@var{n} complex numbers and an
12 operator acting on @var{m} qubits is represented by a 2^@var{m} by
13 2^@var{m} matrix. A hash array @var{qmatrix} is defined with 6 common
14 one-qubit matrices: the identity, the Pauli matrices, the Hadamard
15 matrix and the phase matrix.
17 The major disadvantage compared to a real quantum computer is very slow
18 computing times even with a few qubits. An advantage is that, unlike a
19 quantum computer, in this simulator a quantum state can be cloned using
22 This is an additional package that must be loaded with
23 @code{load("quantum_computing")} in order to use it.
25 @opencatbox{Categories:}
26 @category{Share packages}
27 @category{Package quantum_computing}
30 @node Functions and Variables for Quantum_Computing, , Introduction to quantum_computing, Package quantum_computing
31 @section Functions and Variables for Quantum_Computing
34 @deffn {Function} binlist @
35 @fname{binlist} (@var{k}) @
36 @fname{binlist} (@var{k}, @var{n})
38 @code{binlist}(@var{k}), where @var{k} must be a natural number,
39 returns a list of binary digits 0 or 1 corresponding to the digits of
40 @var{k} in binary representation. @code{binlist}(@var{k}, @var{n}) does
41 the same but returns a list of length @var{n}, with leading zeros as
42 necessary. Notice that for the result to represent a possible state of
43 @var{m} qubits, @var{n} should be equal to 2^@var{m} and @var{k} should
44 be between 0 and 2^@var{m}-1.
46 @opencatbox{Categories:}
47 @category{Package quantum_computing}
53 @deffn {Function} binlist2dec (@var{lst})
55 Given a list @var{lst} with @var{n} binary digits, it returns the decimal
58 @opencatbox{Categories:}
59 @category{Package quantum_computing}
65 @deffn {Function} CNOT (@var{q}, @var{i}, @var{j})
67 Changes the value of the @var{j}'th qubit, in a state @var{q} of @var{m}
68 qubits, when the value of the @var{i}'th qubit equals 1. It modifies the
69 list @var{q} and returns its modified value.
71 @opencatbox{Categories:}
72 @category{Package quantum_computing}
78 @deffn {Function} controlled (@var{U}, @var{q}, @var{c}, @var{i})
80 Applies a matrix @var{U}, acting on @var{m} qubits, on qubits @var{i}
81 through @var{i}+@var{m}-1 of the state @var{q} of @var{n} qubits
82 (@var{n} > @var{m}), when the value of the @var{c}'th qubit in @var{q}
83 equals 1. @var{i} should be an integer between 1 and @var{n}+1-@var{m}
84 and @var{c} should be an integer between 1 and @var{n}, excluding the
85 qubits to be modified (@var{i} through @var{i}+@var{m}-1).
87 @var{U} can be one of the indices of the array of common matrices
88 @var{qmatrix} (see @mref{qmatrix}). The state @var{q} is modified and
91 @opencatbox{Categories:}
92 @category{Package quantum_computing}
98 @deffn {Function} gate @
99 @fname{gate} (@var{U}, @var{q}) @
100 @fname{gate} (@var{U}, @var{q}, @var{i}) @
101 @fname{gate} (@var{U}, @var{q}, @var{i1}, @dots{}, @var{im})
103 @var{U} must be a matrix acting on states of @var{m} qubits; @var{q} a
104 list corresponding to a state of @var{n} qubits (@var{n} >= @var{m});
105 @var{i} and the @var{m} numbers @var{i1}, @dots{}, @var{im} must be
106 different integers between 1 and @var{n}.
108 @code{gate}(@var{U}, @var{q}) applies matrix @var{U} to each qubit of
109 @var{q}, when @var{m} equals 1, or to the first @var{m} qubits of
110 @var{q} when @var{m} is bigger than 1.
112 @code{gate}(@var{U}, @var{q}, @var{i}) applies matrix @var{U} to the
113 qubits @var{i} through @var{i}+@var{m}-1 of @var{q}.
115 @code{gate}(@var{U}, @var{q}, @var{i1}, @dots{}, @var{in}) applies
116 matrix @var{U} to the in the positions @var{i1}, @dots{}, @var{im}.
118 @var{U} can be one of the indices of the array of common matrices
119 @var{qmatrix} (see @mref{qmatrix}). The state @var{q} is modified and
122 @opencatbox{Categories:}
123 @category{Package quantum_computing}
129 @deffn {Function} gate_matrix @
130 @fname{gate_matrix} (@var{U}, @var{n}) @
131 @fname{gate_matrix} (@var{U}, @var{n}, @var{i1}, @dots{}, @var{im})
133 @var{U} must be a 2 by 2 matrix or one of the indices of the array of
134 common matrices @var{qmatrix} (see @mref{qmatrix}).
135 @code{gate_matrix}(@var{U}, @var{n}) returns the matrix corresponding to
136 the action of @var{U} on each qubit in a state of @var{n} qubits.
138 @code{gate_matrix} (@var{U}, @var{n}, @var{i1}, @dots{}, @var{im})
139 returns the matrix corresponding to the action of @var{U} on qubits
140 @var{i1}, @dots{}, @var{im} of a state of @var{n} qubits, where
141 @var{i1}, @dots{}, @var{im} are different integers between 1 and
144 @opencatbox{Categories:}
145 @category{Package quantum_computing}
151 @deffn {Function} linsert (@var{e}, @var{lst}, @var{p})
153 Inserts the expression or list @var{e} into the list @var{lst} at position
154 @var{p}. The list can be empty and @var{p} must be an integer between 1 and
155 the length of @var{lst} plus 1.
157 @opencatbox{Categories:}
158 @category{Package quantum_computing}
164 @deffn {Function} lreplace (@var{e}, @var{lst}, @var{p})
166 If @var{e} is a list of length @var{n}, the elements in the positions
167 @var{p}, @var{p}+1, @dots{}, @var{p}+@var{n}-1 of the list @var{lst} are
168 replaced by @var{e}, or the first elements of @var{e} if the end of
169 @var{lst} is reached. If @var{e} is an expression, the element in
170 position @var{p} of list @var{lst} is replaced by that expression.
171 @var{p} must be an integer between 1 and the length of @var{lst}.
173 @opencatbox{Categories:}
174 @category{Package quantum_computing}
180 @deffn {Function} normalize (@var{q})
182 Returns the normalized version of a quantum state given as a list @var{q}.
184 @opencatbox{Categories:}
185 @category{Package quantum_computing}
191 @deffn {Function} qdisplay (@var{q})
193 Represents the state @var{q} of a system of @var{n} qubits as a linear
194 combination of the computational states with @var{n} binary digits. It
195 returns an expression including strings and symbols.
197 @opencatbox{Categories:}
198 @category{Package quantum_computing}
204 @defvr {System variable} qmatrix
206 This variable is a predefined hash array of two by two matrices with the
207 standard matrices: identity, Pauli matrices, Hadamard matrix and the
208 phase matrix. The six possible indices are I, X, Y, Z, H,
209 S. @var{qmatrix}[I] is the identity matrix, @var{qmatrix}[X] the Pauli x
210 matrix, @var{qmatrix}[Y] the Pauli y matrix, @var{qmatrix}[Z] the Pauli
211 z matrix, @var{qmatrix}[H] the Hadamard matrix and @var{qmatrix}[S] the
214 @opencatbox{Categories:}
215 @category{Package quantum_computing}
221 @deffn {Function} qmeasure @
222 @fname{qmeasure} (@var{q}) @
223 @fname{qmeasure} (@var{q}, @var{i1}, @dots{}, @var{im})
225 Measures the value of one or more qubits in a system of @var{n} qubits
226 with state @var{q}. The @var{m} positive integers @var{i1}, @dots{},
227 @var{im} are the positions of the qubits to be measured It requires 1 or
228 more arguments. The first argument must be the state q. If the only
229 argument given is @var{q}, all the n qubits will be measured.
231 It returns a list with the values of the qubits measured (either 0 or
232 1), in the same order they were requested or in ascending order if the
233 only argument given was @var{q}. It modifies the list @var{q},
234 reflecting the collapse of the quantum state after the measurement.
236 @opencatbox{Categories:}
237 @category{Package quantum_computing}
243 @deffn {Function} qubits @
244 @fname{qubits} (@var{n}) @
245 @fname{qubits} (@var{i1}, @dots{}, @var{in})
247 @code{qubits}(@var{n}) returns a list representing the ground state of a
248 system of @var{n} qubits.
250 @code{qubits}(@var{i1}, @dots{}, @var{in}) returns a list with
251 representing the state of @var{n} qubits with values @var{i1}, @dots{},
254 @opencatbox{Categories:}
255 @category{Package quantum_computing}
261 @deffn {Function} qswap (@var{q}, @var{i}, @var{j})
263 Interchanges the states of qubits @var{i} and @var{j} in the state
264 @var{q} of a system of several qubits. It modifies the list @var{q} and
265 returns its modified value.
267 @opencatbox{Categories:}
268 @category{Package quantum_computing}
274 @deffn {Function} Rx (@var{a})
276 Returns the 2 by two matrix (acting on one qubit) corresponding to a
277 rotation of with an angle of @var{a} radians around the x axis.
279 @opencatbox{Categories:}
280 @category{Package quantum_computing}
286 @deffn {Function} Ry (@var{a})
288 Returns the 2 by two matrix (acting on one qubit) corresponding to a
289 rotation of with an angle of @var{a} radians around the y axis.
291 @opencatbox{Categories:}
292 @category{Package quantum_computing}
298 @deffn {Function} Rz (@var{a})
300 Returns the 2 by two matrix (acting on one qubit) corresponding to a
301 rotation of with an angle of @var{a} radians around the z axis.
303 @opencatbox{Categories:}
304 @category{Package quantum_computing}
310 @deffn {Function} tprod (@var{o1}, @dots{}, @var{on})
312 Returns the tensor product of the @var{n} matrices or lists @var{o1},
315 @opencatbox{Categories:}
316 @category{Package quantum_computing}
322 @deffn {Function} toffoli (@var{q}, (@var{i}, (@var{j}, (@var{k})
324 Changes the value of the @var{k}'th qubit, in the state @var{q} of
325 @var{n} qubits, if the values of the @var{i}'th anf @var{j}'th qubits
326 are equal to 1. It modifies the list @var{q} and returns its new value.
328 @opencatbox{Categories:}
329 @category{Package quantum_computing}