Circuit package

Module contents

Circuits allow to simulate the computation on a quantum computer.

The circuit is a sequence of quantum gates that are applied to the qubits. Since the simulator runs on a classical computer, the gates are applied using matrix multiplication, and the state of all qubits is represented as a vector.

When the qubits are measured, the state of the qubits collapses using a random number generator that follows the probability distribution of the state vector.

bell module

BellCircuit class.

class qclight.circuit.bell.BellCircuit(state)[source]

Bases: QCLCircuit

Bell states are the four states that can be created when two qubits are maximally entangled. In this state, the behavior of one qubit affects the other one, and vice versa.

\[\begin{split}\begin{align} \ket{\Phi^+} = \frac{\ket{00} + \ket{11}}{\sqrt{2}} \\ \ket{\Phi^-} = \frac{\ket{00} - \ket{11}}{\sqrt{2}} \\ \ket{\Psi^+} = \frac{\ket{01} + \ket{10}}{\sqrt{2}} \\ \ket{\Psi^-} = \frac{\ket{01} - \ket{10}}{\sqrt{2}} \end{align}\end{split}\]
correlate(q1, q2)[source]

Correlates the two qubits provided. The bell state is created by

  • applying a HGate gate to the first qubit

  • applying a cx gate between the two qubits

Parameters
  • q1 (int) – first qubit to correlate

  • q2 (int) – second qubit to correlate

Return type

None

boolean_inner_product module

BooleanInnerProductCircuit class.

class qclight.circuit.boolean_inner_product.BooleanInnerProductCircuit(a, b)[source]

Bases: QCLCircuit

The boolean inner product is defined as the bitwise AND of the two vectors followed by the XOR bit by bit of the result.

\[\begin{align} x \cdot y = \underbrace{(x_1 \land y_1) \oplus (x_2 \land y_2) \oplus \dots \oplus (x_n \land y_n)}_{n} \end{align}\]
inner_product()[source]

Computes the boolean inner product of the input vectors and returns the result.

Returns

bool – result obtained by computing the boolean inner product

circuit module

QCLCircuit class

class qclight.circuit.circuit.QCLCircuit(state)[source]

Bases: object

Quantum circuit used for a generic computation

ccx(c1, c2, t)[source]

Applies a ccx gate controlled by both qubits in position c1 and c2. The qubit in position t will be negated if both control qubits are 1.

Truth table of the CCX gate.

c1

c2

t

CCX

0

0

0

0

0

1

0

0

1

0

0

0

1

1

0

1

0

0

1

1

0

1

1

1

1

0

1

1

1

1

1

0

Parameters
  • c1 (int) – position of the first qubit controlling the gate

  • c2 (int) – position of the second qubit controlling the gate

  • t (int) – position of the qubit to be affected by the gate

Return type

None

counts(auto_run=True, msr_list=None)[source]

Shows the result of the computation and the probability for each to happen. If auto_run is True, the circuit is run before showing the result. If an msr_list is provided, the result is shown only for the qubits with that index.

Parameters
  • auto_run (bool, default: True) – whether to run the circuit before showing the result

  • msr_list (Optional[list[int]], default: None) – list of indices of the qubits to be considered

Return type

None

cx(c, t)[source]

Applies a cx gate controlled by the qubit in position c. The qubit in position t will be negated if the control qubit is 1.

Truth table of the CX gate.

c

t

CX

0

0

0

0

1

1

1

0

1

1

1

0

Parameters
  • c (int) – position of the qubit controlling the gate

  • t (int) – position of the qubit to be affected by the gate

Return type

None

h(i)[source]

Applies a HGate gate to the qubit in position i.

Parameters

i (int | list[int]) – position of the qubit to be affected by the gate

Return type

None

initialize_circuit(state)[source]

Builds a circuit that output the provided state starting from the all-zero state.

Parameters

state (str) – state to be outputted

Return type

None

mcx(c_bits, t)[source]

Applies a mcx gate controlled by all the qubits in position c_bits. The qubit in position t will be negated if all control qubits are 1.

\[\begin{split}mcx(c_1, c_2, ..., c_n, t) = \begin{cases} \lnot t & \text{if } c_1 \land c_2 \land \dots \land c_n \\ t & \text{otherwise} \\ \end{cases}\end{split}\]
Parameters
  • c_bits (Iterable[int]) – positions of the qubits controlling the gate

  • t (int) – position of the qubit to be affected by the gate

Return type

None

measure(auto_run=True, msr_list=None)[source]

Collapses the qubits and show their value as a classical bit. If auto_run is True, the circuit is run before showing the result. If an msr_list is provided, the result is shown only for the qubits with that index.

Parameters
  • auto_run (bool, default: True) – whether to run the circuit before showing the result

  • msr_list (Optional[list[int]], default: None) – list of indices of the qubits to be measured

Return type

None

property n: int

Number of qubits in the circuit.

Return type

int

or_(q1, q2, t)[source]

Applies an OR gate to the qubits in position q1 and q2. The result will be stored in the qubit in position t.

Truth table of the OR gate.

q1

q2

OR

0

0

0

0

1

1

1

0

1

1

1

1

Return type

None

property result: ndarray[Any, dtype[float64]]

Result of the last simulation or the initial state if no simulation has been run yet.

Return type

ndarray[Any, dtype[float64]]

run()[source]

Runs the simulator and returns the result.

Returns

ndarray[Any, dtype[float64]] – results of the computation

property state: ndarray[Any, dtype[float64]]

Initial state of the circuit.

Return type

ndarray[Any, dtype[float64]]

swap(i, j)[source]

Swaps the qubit in position i with the qubit in position j.

Parameters
  • i (int) – position of the first qubit to be swapped

  • j (int) – position of the second qubit to be swapped

Return type

None

x(i)[source]

Applies a XGate gate to the qubit in position i.

Truth table of the X gate.

i

X

0

1

1

0

Parameters

i (int | list[int]) – position of the qubit to be affected by the gate

Return type

None

half_adder module

HalfAdderCircuit class

class qclight.circuit.half_adder.HalfAdderCircuit(a, b)[source]

Bases: QCLCircuit

Half adder circuit. Allows to sum two bits and return the result, as well as the carry bit.

Truth table of the half adder circuit.

a

b

sum

carry

0

0

0

0

0

1

1

0

1

0

1

0

1

1

0

1

sum()[source]

Sums the input numbers and returns the result. Runs the quantum circuit and return the certain output as an integer.

Returns

int – result obtained by summing the input numbers

random module

RandomCircuit class

class qclight.circuit.random.RandomCircuit(n)[source]

Bases: QCLCircuit

Using the true randomness of the qubits, it is possible to generate a true random number between 0 and 2^n - 1.

It is sufficient to use apply the HGate gate to n qubits to produce a uniform superposition of all possible states.

visual_circuit module

QCLCircuit class

class qclight.circuit.visual_circuit.QCLVisualCircuit(state)[source]

Bases: QCLCircuit

Quantum circuit used for a generic computation. This subclass of QCLCircuit adds visualization capabilities to the circuit. To print the circuit, simply call the __str__() method.

barrier(qubits=None)[source]

Adds a barrier in the visualization of the circuit. It can help to separate different parts of the circuit.

Parameters

qubits (Optional[Iterable[int]], default: None) – qubits to which the barrier is applied

Return type

None

ccx(c1, c2, t)[source]

Applies a ccx gate controlled by both qubits in position c1 and c2. The qubit in position t will be negated if both control qubits are 1.

Truth table of the CCX gate.

c1

c2

t

CCX

0

0

0

0

0

1

0

0

1

0

0

0

1

1

0

1

0

0

1

1

0

1

1

1

1

0

1

1

1

1

1

0

Parameters
  • c1 (int) – position of the first qubit controlling the gate

  • c2 (int) – position of the second qubit controlling the gate

  • t (int) – position of the qubit to be affected by the gate

Return type

None

cx(c, t)[source]

Applies a cx gate controlled by the qubit in position c. The qubit in position t will be negated if the control qubit is 1.

Truth table of the CX gate.

c

t

CX

0

0

0

0

1

1

1

0

1

1

1

0

Parameters
  • c (int) – position of the qubit controlling the gate

  • t (int) – position of the qubit to be affected by the gate

Return type

None

h(i)[source]

Applies a HGate gate to the qubit in position i.

Parameters

i (int | list[int]) – position of the qubit to be affected by the gate

Return type

None

mcx(c_bits, t)[source]

Applies a mcx gate controlled by all the qubits in position c_bits. The qubit in position t will be negated if all control qubits are 1.

\[\begin{split}mcx(c_1, c_2, ..., c_n, t) = \begin{cases} \lnot t & \text{if } c_1 \land c_2 \land \dots \land c_n \\ t & \text{otherwise} \\ \end{cases}\end{split}\]
Parameters
  • c_bits (Iterable[int]) – positions of the qubits controlling the gate

  • t (int) – position of the qubit to be affected by the gate

Return type

None

property n: int

Number of qubits in the circuit.

Return type

int

x(i)[source]

Applies a XGate gate to the qubit in position i.

Truth table of the X gate.

i

X

0

1

1

0

Parameters

i (int | list[int]) – position of the qubit to be affected by the gate

Return type

None