qrisp.QuantumCircuit.statevector_array#

QuantumCircuit.statevector_array()[source]#

Simulate the circuit statevector and return it as a NumPy array of complex amplitudes.

Note

The returned array uses big-endian index ordering. The array index i maps to qubit values as

\[i = \sum_{k=0}^{n-1} q_k \, 2^{\,n-1-k},\]

so \(q_0\) is the most significant qubit. For two qubits this yields:

  • i = 0\(|q_0=0, q_1=0\rangle\)

  • i = 1\(|q_0=0, q_1=1\rangle\)

  • i = 2\(|q_0=1, q_1=0\rangle\)

  • i = 3\(|q_0=1, q_1=1\rangle\)

This differs from Qrisp’s internal little-endian convention (only the index-to-basis mapping changes).

Returns:
numpy.ndarray

The statevector of this circuit in big-endian order.

Examples

We create a QuantumCircuit, perform some operations and retrieve the statevector array.

>>> from qrisp import QuantumCircuit
>>> qc = QuantumCircuit(4)
>>> qc.h(qc.qubits)
>>> qc.z(-1)
>>> qc.statevector_array()
array([ 0.24999997+0.j, -0.24999997+0.j,  0.24999997+0.j, -0.24999997+0.j,
        0.24999997+0.j, -0.24999997+0.j,  0.24999997+0.j, -0.24999997+0.j,
        0.24999997+0.j, -0.24999997+0.j,  0.24999997+0.j, -0.24999997+0.j,
        0.24999997+0.j, -0.24999997+0.j,  0.24999997+0.j, -0.24999997+0.j],
      dtype=complex64)

In this example, we create a QuantumFloat and prepare the normalized state \(\sum_{i=0}^3 \tilde b_i\ket{i}\) for \(\tilde b=(0,1,2,3)/\sqrt{14}\).

>>> import numpy as np
>>> from qrisp import QuantumFloat
>>> b = np.array([0, 1, 2, 3], dtype=float)
>>> b /= np.linalg.norm(b)
>>> qf = QuantumFloat(2)
>>> qf.init_state(b)
>>> sv_array = qf.qs.statevector_array()
>>> print(f"b[1]: {b[1]:.6f} -> {sv_array[2]:.6f}")
b[1]: 0.267261 -> 0.267261-0.000000j
>>> print(f"b[2]: {b[2]:.6f} -> {sv_array[1]:.6f}")
b[2]: 0.534522 -> 0.534522-0.000000j

Here sv_array[2] corresponds to \(\ket{q_0=1, q_1=0}\) and sv_array[1] to \(\ket{q_0=0, q_1=1}\).