Qubit Class

class qubit.Qubit(alpha: complex, beta: complex)

Bases: object

Variables:
  • alpha (complex) – The amplitude of the \(|0⟩\) state, a value between 0 and 1.

  • beta (complex) – The amplitude of the \(|1⟩\) state, a value between 0 and 1.

A class to represent a quantum bit (Qubit) with two possible states: \(|0⟩\) and \(|1⟩\), described by complex amplitudes alpha and beta.

get_alpha() complex

Returns the amplitude of the \(|0⟩\) state.

Returns:

The amplitude of the \(|0⟩\) state (alpha).

Return type:

complex

get_beta() complex

Returns the amplitude of the \(|1⟩\) state.

Returns:

The amplitude of the \(|1⟩\) state (beta).

Return type:

complex

get_vector() ndarray[Any, dtype[complex128]]

Returns the qubit in vector form.

Returns:

The vector form of the qubit (__qubit_vector).

Return type:

np.ndarray

measure() int

Measure the qubit and return the collapsed state.

Simulates a measurement in the computational basis (0 state and 1 state) by calculating the probabilities of each state and using a random number in a uniform distribution on [0,1] interval to determine the outcome.

Returns:

The measurement result: 0 or 1.

Return type:

int

Notes

The probabilities of each state are determined by the amplitudes squared of each qubit.

Example

>>> qubit = Qubit(0.6, 0.8)
>>> result = qubit.measure()
>>> print(result)  # Output will be '0' with probability 0.36, or '1' with probability 0.64
print_qubit() None

Prints the qubit state in the form: Qubit state is \(α\exp{i\phi_{0}}|0⟩\) + \(β\exp{i\phi_{1}}|1⟩\).

The phase terms are included only if their corresponding relative phases are non-zero.

print_vector_form() None

Prints the qubit state in the vector form: Qubit state is [alpha,beta].

set_amplitudes(alpha: complex, beta: complex) None

Sets the amplitude of the \(|0⟩\) state and \(|1⟩\) state.

Parameters:
  • alpha (complex) – The amplitude of the \(|0⟩\) state, should be a complex number with amplitude between 0 and 1.

  • beta (complex) – The amplitude of the \(|1⟩\) state, should be a complex number with amplitude between 0 and 1.

Raises:

ValueError – If the given amplitudes do not satisfy the normalization condition.