Multi Qubit Class

class multi_qubit.MultiQubit(vector: ndarray[Any, dtype[complex128]] = array([], dtype=float64))

Bases: object

Variables:
  • __number_of_qubits (int) – Number of qubits in the tensor product.

  • __tensor_vector (list[complex]) – Vector representation of the quantum state.

A class representing tensor products of many quantum bits (qubits).

This class implements the tensor product operation for quantum states, allowing for the construction and manipulation of multi-qubit systems. It maintains both the vector representation and the quantum state notation.

Example

>>> mt = MultiQubit()
>>> q0 = Qubit(1,0)
>>> q1 = Qubit(0,1)
>>> mt.add_qubit(q0)
>>> mt.add_qubit(q1)
>>> mt.print_tensor_form()
Tensor product in basis state form: |01⟩
add_qubit(new_qubit: Qubit) None

Add a new qubit to the tensor product and update the state vector.

This method adds a new qubit to the system and updates the tensor product state vector accordingly.

Parameters:

new_qubit (Qubit) – The qubit to be added to the tensor product

Raises:

TypeError – If new_qubit is not a Qubit object

Example

>>> mt = MultiQubit()
>>> q0 = Qubit(1,0)  # |0⟩ state
>>> mt.add_qubit(q0)
get_number_of_qubits() int

Get the number of qubits in the tensor product.

Returns:

The number of qubits in the system

Return type:

int

get_qubit(qubit_index: int) Qubit

Returns the qubit we want to trace out from the quantum state of multiple qubits.

The function computes the sum of the amplitudes of the projection of a specific qubit in the given quantum state on the |0⟩ and |1⟩ states.

Parameters:

qubit_index (int) – The index of the qubit whose amplitudes are to be extracted.

Returns:

An object of class Qubit representing the amplitudes for the |0⟩ and |1⟩ states.

Return type:

Qubit

Raises:

ValueError – If the qubit_index is not valid.

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

Get the tensor product vector.

Returns:

Tensor product vector

Return type:

np.ndarray

measure(return_as_str: bool = True) str | Self

This function measures the state one time and returns the state to which the mesurement collapsed to. The return type can be an str or a MultiQubit object.

Parameters:

return_as_str (bool) – Boolean argument. True if to return as a string and false a multiqubit object.

Returns:

  • str – The state to which the measurement collapsed to in string format.

  • MultQubit – The state to which the measurement collapsed to as a MultiQubit object.

measure_qubit(qubit_index: int) tuple[Self, int]

This method measures a specified qubit inside a multi-qubit quantum state and returns the new quantum state after the collapse along with the measured state (0 or 1).

This measurement is called a partial measurement and is equivalent to tracing out all other qubits and measuring the desired qubits without changing the other qubits.

Parameters:

qubit_index (int) – The qubit index we wish to measure.

Returns:

A tuple containing: - MultiQubit: The collapsed state after measuring a particular qubit. - int: The measurement result (0 or 1).

Return type:

tuple

Raises:

ValueError – If the qubit_index is not valid. Qubit index should be between 0 and number of qubits - 1 and an integer.

Examples

>>> vector = np.full(16, 1/4)
>>> mt = MultiQubit(vector)
>>> mt.print_tensor_form()
>>> for i in range(4):
...     mt, outcome = mt.measure_qubit(i)
...     print(f"Measured qubit {i}: {outcome}")
...     mt.print_tensor_form()
Output:
Tensor product in basis state form: 0.5|0000⟩ + 0.5|0001⟩ + 0.5|0010⟩ + 0.5|0011⟩
Measured qubit 0: 0
Tensor product in basis state form: ...
plot_amplitudes(plot_type: str = 'bar') None

This function plots the amplitudes for the quantum state either as a bar chart or a regular plot.

Parameters:

plot_type (str, optional) – The type of plot to display. Can be ‘bar’ for a bar chart or ‘line’ for a regular plot (default is ‘bar’).

Return type:

None

plot_measurements(num_of_measurements: int = 10000) None

This function measures a specified number of times. Then plots in a graph the number of times we got each state divided by the number of measurements thus resulting in the probabilities.

Parameters:

number_of_measurements (int) – The number of times to measure the entire circuit.

plot_probabilities() None

This function plots directly the probabilities for the the state with measuring.

print_tensor_form() None

Print the quantum state in Dirac notation.

Displays the quantum state as a sum of computational basis states with their corresponding complex coefficients.

print_vector_form() None

Print the vector representation of the quantum state.

Displays the coefficients of the quantum state in the computational basis as a list of complex numbers.

str_to_state(input_str: str) Self

This method converts a string which represents a single quantum state and returns the state as MultiQubit object.

Parameters:

input_str (str) – The input state as a string.

Returns:

The input state as a MultiQubit objects.

Return type:

MultiQubit