mahout

qumat

QuMat Objects

class QuMat()

Unified interface for quantum circuit operations across multiple backends.

Provides a consistent API for creating and manipulating quantum circuits using different quantum computing backends (Qiskit, Cirq, Amazon Braket). Abstracts backend-specific details for gate operations, circuit execution, and state measurement.

Arguments:

__init__

def __init__(backend_config)

Create a QuMat instance with the specified backend configuration.

Arguments:

Raises:

create_empty_circuit

def create_empty_circuit(num_qubits: int | None = None)

Create an empty quantum circuit with the specified number of qubits.

Must be called before applying any gates or executing operations.

Arguments:

apply_not_gate

def apply_not_gate(qubit_index)

Apply a NOT gate (Pauli-X gate) to the specified qubit.

Flips the qubit state from |0⟩ to |1⟩ or |1⟩ to |0⟩. Equivalent to the Pauli-X gate.

Arguments:

Raises:

apply_hadamard_gate

def apply_hadamard_gate(qubit_index)

Apply a Hadamard gate to the specified qubit.

Creates a superposition state, transforming 0⟩ to ( 0⟩ + 1⟩)/√2
and 1⟩ to ( 0⟩ - 1⟩)/√2.

Arguments:

Raises:

apply_cnot_gate

def apply_cnot_gate(control_qubit_index, target_qubit_index)

Apply a Controlled-NOT (CNOT) gate between two qubits.

Fundamental for entangling qubits. Flips the target qubit if and only if the control qubit is in the |1⟩ state.

Arguments:

Raises:

apply_toffoli_gate

def apply_toffoli_gate(control_qubit_index1, control_qubit_index2,
                       target_qubit_index)

Apply a Toffoli gate (CCX gate) to three qubits.

Acts as a quantum AND gate. Flips the target qubit if and only if both control qubits are in the |1⟩ state.

Arguments:

Raises:

apply_swap_gate

def apply_swap_gate(qubit_index1, qubit_index2)

Swap the states of two qubits.

Arguments:

Raises:

apply_cswap_gate

def apply_cswap_gate(control_qubit_index, target_qubit_index1,
                     target_qubit_index2)

Apply a controlled-SWAP (Fredkin) gate.

Swaps the states of two target qubits if and only if the control qubit is in the |1⟩ state.

Arguments:

Raises:

apply_pauli_x_gate

def apply_pauli_x_gate(qubit_index)

Apply a Pauli-X gate to the specified qubit.

Equivalent to the NOT gate. Flips the qubit state from 0⟩ to 1⟩
or 1⟩ to 0⟩.

Arguments:

Raises:

apply_pauli_y_gate

def apply_pauli_y_gate(qubit_index)

Apply a Pauli-Y gate to the specified qubit.

Rotates the qubit around the Y-axis of the Bloch sphere, affecting both phase and amplitude.

Arguments:

Raises:

apply_pauli_z_gate

def apply_pauli_z_gate(qubit_index)

Apply a Pauli-Z gate to the specified qubit.

Rotates the qubit around the Z-axis of the Bloch sphere, altering the phase without changing the amplitude.

Arguments:

Raises:

apply_t_gate

def apply_t_gate(qubit_index)

Apply a T-gate (π/8 gate) to the specified qubit.

Applies a relative pi/4 phase (multiplies the |1> state by e^{i*pi/4}). Essential for universal quantum computation when combined with Hadamard and CNOT gates.

Arguments:

Raises:

execute_circuit

def execute_circuit(parameter_values=None)

Execute the quantum circuit and return the measurement results.

Runs the circuit on the configured backend. For parameterized circuits, provide parameter values to bind before execution.

Arguments:

Raises:

Returns:

dict | list[dict]: Measurement results. Format depends on the backend:

bind_parameters

def bind_parameters(parameter_values)

Bind numerical values to circuit parameters.

Assigns numerical values to symbolic parameters defined in parameterized gates.

Arguments:

Raises:

get_final_state_vector

def get_final_state_vector()

Return the final state vector of the quantum circuit.

The complete quantum state vector after circuit execution, representing the full quantum state of all qubits. For parameterized circuits, call bind_parameters() first to set parameter values.

Raises:

Returns:

numpy.ndarray: The final state vector as a numpy array.

draw_circuit

def draw_circuit()

Visualize the quantum circuit.

Generates a visual representation of the circuit. The output format depends on the backend implementation.

Raises:

Returns:

str | object: Circuit visualization. The exact type depends on the backend.

draw

def draw()

Alias for draw_circuit() for convenience.

Provides a shorter method name that matches common quantum computing library conventions and documentation examples.

Raises:

Returns:

str | object: Circuit visualization. The exact type depends on the backend.

apply_rx_gate

def apply_rx_gate(qubit_index, angle)

Apply a rotation around the X-axis to the specified qubit.

Rotates the qubit by the given angle around the X-axis of the Bloch sphere. The angle can be a static value or a parameter name for parameterized circuits.

Arguments:

Raises:

apply_ry_gate

def apply_ry_gate(qubit_index, angle)

Apply a rotation around the Y-axis to the specified qubit.

Rotates the qubit by the given angle around the Y-axis of the Bloch sphere. The angle can be a static value or a parameter name for parameterized circuits.

Arguments:

Raises:

apply_rz_gate

def apply_rz_gate(qubit_index, angle)

Apply a rotation around the Z-axis to the specified qubit.

Rotates the qubit by the given angle around the Z-axis of the Bloch sphere. The angle can be a static value or a parameter name for parameterized circuits.

Arguments:

Raises:

apply_u_gate

def apply_u_gate(qubit_index, theta, phi, lambd)

Apply a U gate (universal single-qubit gate) to the specified qubit.

A universal single-qubit gate parameterized by three angles (theta, phi, lambd) that can represent any single-qubit unitary operation.

Arguments:

Raises:

swap_test

def swap_test(ancilla_qubit, qubit1, qubit2)

Implement the swap test circuit for measuring overlap between two quantum states.

Measures the inner product between the states on qubit1 and qubit2. The probability of measuring the ancilla qubit in state |0⟩ is related to the overlap as: P(0) = (1 + |⟨ψ|φ⟩|²) / 2

Arguments:

Raises:

measure_overlap

def measure_overlap(qubit1, qubit2, ancilla_qubit=0)

Measure the overlap (fidelity) between two quantum states using the swap test.

Creates a swap test circuit to calculate the similarity between the quantum states on qubit1 and qubit2. Returns the squared overlap |⟨ψ|φ⟩|², which represents the fidelity between the two states.

The swap test measures P(ancilla=0), related to overlap as: P(0) = (1 + |⟨ψ|φ⟩|²) / 2

For certain states (especially identical excited states), global phase effects may cause the ancilla to measure predominantly |1⟩ instead of |0⟩. This method handles both cases by taking the measurement probability closer to 1.

Arguments:

Raises:

Returns:

float: The squared overlap |⟨ψ|φ⟩|² between the two states (fidelity), clamped to the range [0.0, 1.0].

calculate_prob_zero

def calculate_prob_zero(results, ancilla_qubit)
Calculate the probability of measuring the ancilla qubit in 0⟩ state.

Delegates to the backend-specific implementation. Different backends may use different qubit ordering conventions (little-endian vs big-endian).

Arguments:

Returns:

float: Probability of measuring the ancilla qubit in 0⟩ state.