gquantum package

Submodules

gquantum.backend module

This module contains internal operations on amplitudes of qubits.

gquantum.qubit module

This module contains initialization and operation on qubits.

This module contains all main quantum gates and measurements on quantum computation. Initializes the class Qubit with the number of required qubits, and then uses the quantum gates and measurements as functions inside the class.

class gquantum.qubit.Qubit(num_qubits)

Bases: object

Creates qubits register.

num_qubits

The number of qubits in register.

amplitudes

The amplitudes of qubits in register.

ccnot(control_index_1, control_index_2, target_index)

Applies the CCNOT(toffoli) gate to three qubits.

Parameters:
  • control_index_1 – Index of the first control qubit, starts from 0.
  • control_index_2 – Index of the second control qubit, starts from 0.
  • target_index – Index of the target qubit, starts from 0.
cnot(control_index, target_index)

Applies the controlled-NOT(CNOT) gate to a pair of qubits.

Parameters:
  • control_index – Index of the control qubit, starts from 0.
  • target_index – Index of the target qubit, starts from 0.
cx(control_index, target_index)

Applies the controlled-NOT(CX) gate to a pair of qubits.

Parameters:
  • control_index – Index of the control qubit, starts from 0.
  • target_index – Index of the target qubit, starts from 0.
h(qubit_index)

Applies the Hadamard transformation to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
id(qubit_index)

Applies the Identity gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
measure(qubit_index)

Performs a measurement of a single qubit in computational(Pauli Z) basis.

Parameters:qubit_index – Index of qubit to be measured, starts from 0.
Returns:“0” or “1” as type string. Represent the state |0> and |1> .
measure_x(qubit_index)

Performs a measurement of a single qubit in Pauli X basis.

Parameters:qubit_index – Index of qubit to be measured, starts from 0.
Returns:“0” or “1” as type string. Represent the state |0> and |1> .
measure_y(qubit_index)

Performs a measurement of a single qubit in Pauli Y basis.

Parameters:qubit_index – Index of qubit to be measured, starts from 0.
Returns:“0” or “1” as type string. Represent the state |0> and |1> .
measure_z(qubit_index)

Performs a measurement of a single qubit in Pauli Z(computational) basis.

Parameters:qubit_index – Index of qubit to be measured, starts from 0.
Returns:“0” or “1” as type string. Represent the state |0> and |1> .
multi_controlled_gate(gate, qubit_index, control_index_list)

Applies a specific gate to a qubit with controls of other qubits.

Parameters:
  • gate – Specific gate to be executed. comes from “X, Y, Z, H, S, T, Id, SDagger, TDagger”.
  • qubit_index – Index of the target qubit, starts from 0.
  • control_index_list – List of indices of the control qubits, the index in this list should starts from 0.
multi_controlled_p(theta, qubit_index, control_index_list)

Applies the phase shift gate to a qubit with controls of other qubits.

The phase shift gate manipulates a qubit as a rotation with an angle theta about Z-axis, which is the same as the RZ gate together with a global phase shift.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of the target qubit, starts from 0.
  • control_index_list – List of indices of the control qubits, the index in this list starts from 0.
multi_controlled_rx(theta, qubit_index, control_index_list)

Applies the RX gate to a qubit with controls of other qubits.

The RX gate manipulates a qubit as a rotation with an angle theta about X-axis.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of the target qubit, starts from 0.
  • control_index_list – List of indices of the control qubits, the index in this list should starts from 0.
multi_controlled_ry(theta, qubit_index, control_index_list)

Applies the RY gate to a qubit with controls of other qubits.

The RY gate manipulates a qubit as a rotation with an angle theta about Y-axis.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of the target qubit, starts from 0.
  • control_index_list – List of indices of the control qubits, the index in this list should starts from 0.
multi_controlled_rz(theta, qubit_index, control_index_list)

Applies the RZ gate to a qubit with controls of other qubits.

The RZ gate manipulates a qubit as a rotation with an angle theta about Z-axis.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of the target qubit, starts from 0.
  • control_index_list – List of indices of the control qubits, the index in this list should starts from 0.
multi_qubit_measure(qubit_index_list)

Performs measurements of qubits in computational(Pauli Z) basis.

Parameters:qubit_index_list – List of indices of qubits to be measured, the index in this list should starts from 0.
Returns:A list with elements “0” or “1” as type string, which represent the state |0> and |1> for qubits with the descending order.

example:

[‘1’, ‘0’, ‘0’]

The ‘1’ represents the state |1> for the qubit with the biggest index.

p(theta, qubit_index)

Applies the phase shift gate to a qubit.

The single qubit phase shift gate manipulates a qubit as a rotation with an angle theta about Z-axis, which is the same as the RZ gate together with an unobservable global phase shift.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of qubit to which the gate should be applied, starts from 0.
reset(qubit_index)

Reset a qubit to |0>.

When the qubit is entangled with other qubits, those qubits would also collapse with the role of quantum measurement.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
reset_all()

Reset all qubits to |0>.

rx(theta, qubit_index)

Applies the RX gate to a qubit.

The RX gate manipulates a qubit as a rotation with an angle theta about X-axis.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of qubit to which the gate should be applied, starts from 0.
ry(theta, qubit_index)

Applies the RY gate to a qubit.

The RY gate manipulates a qubit as a rotation with an angle theta about Y-axis.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of qubit to which the gate should be applied, starts from 0.
rz(theta, qubit_index)

Applies the RZ gate to a qubit.

The RZ gate manipulates a qubit as a rotation with an angle theta about Z-axis.

Parameters:
  • theta – Angle about which the qubit is to be rotated.
  • qubit_index – Index of qubit to which the gate should be applied, starts from 0.
s(qubit_index)

Applies the π/4 phase gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
s_dagger(qubit_index)

Applies the adjoint of S gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
simulator_func_get_amplitudes()

Returns the amplitudes of this quantum register.

This function is not directly performable on a real quantum computer.

simulator_func_load_amplitudes(file='amplitudes.npy')

Load the amplitudes of this quantum register to a file.

This function is not directly performable on a real quantum computer.

Parameters:file – Path to the file to load amplitudes.
simulator_func_multi_measure_without_collapse(qubit_index_list, measure_times)

Performs measurements several times without collapse.

This function is not directly performable on a real quantum computer.

Parameters:
  • qubit_index_list – List of indices of qubits to be measured, the index in this list should starts from 0.
  • measure_times – Number of times to perform measurements.
Returns:

A dict with keys as measured states and values as numbers of times measured in that states. The qubits represented are in descending order.

example:

{‘00’: 490, ‘11’: 510}

simulator_func_save_amplitudes(file='amplitudes.npy')

Save the amplitudes of this quantum register to a file.

This function is not directly performable on a real quantum computer.

Parameters:file – Path to the file to save amplitudes.
swap(qubit_1_index, qubit_2_index)

Applies the SWAP gate to a pair of qubits.

Parameters:
  • qubit_1_index – Index of the first qubit to be swapped, starts from 0.
  • qubit_2_index – Index of the first qubit to be swapped, starts from 0.
t(qubit_index)

Applies the π/8 phase gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
t_dagger(qubit_index)

Applies the adjoint of T gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
toffoli(control_index_1, control_index_2, target_index)

Applies the toffoli(CCNOT) gate to three qubits.

Parameters:
  • control_index_1 – Index of the first control qubit, starts from 0.
  • control_index_2 – Index of the second control qubit, starts from 0.
  • target_index – Index of the target qubit, starts from 0.
x(qubit_index)

Applies the Pauli X gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
y(qubit_index)

Applies the Pauli Y gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.
z(qubit_index)

Applies the Pauli Z gate to a qubit.

Parameters:qubit_index – Index of qubit to which the gate should be applied, starts from 0.

gquantum.utils module

This module contains public GQuantum utilities.

This module contains all public GQuantum utilities which consist of assembled operations on the class Qubit.

gquantum.utils.iqft(qubits, index_list)

Applies inverse quantum Fourier transformation to a list of qubits.

The inverse Fourier transformation is consistent with the most conventional notation, which does not have a “-” sign on the imaginary part.

Parameters:
  • qubits – Qubit object which contains all qubits to be transformed.
  • index_list – List of indices of the qubits to be transformed. The transformation operates according to the list order, and the index in this list starts from 0.
gquantum.utils.qft(qubits, index_list)

Applies quantum Fourier transformation to a list of qubits.

The Fourier transformation is consistent with the most conventional notation, which has a “-” sign on the imaginary part.

Parameters:
  • qubits – Qubit object which contains all qubits to be transformed.
  • index_list – List of indices of the qubits to be transformed. The transformation operates according to the list order, and the index in this list starts from 0.

Module contents