Skip to content

Large Statevector ​

A 20-qubit exact statevector: a million complex amplitudes, 16 MiB.

py
"""A larger exact statevector: 20 qubits, about 16 MiB of amplitudes."""

import math
import random

import numpy as np
from qiskit import QuantumCircuit

from menos.qiskit import MENOSBackend

n = 20
rng = random.Random(3)
qc = QuantumCircuit(n)
for _ in range(3):
    for q in range(n):
        qc.ry(rng.uniform(0, math.pi), q)
    for q in range(0, n - 1, 2):
        qc.cx(q, q + 1)
    for q in range(1, n - 1, 2):
        qc.cx(q, q + 1)

state = np.asarray(
    MENOSBackend().run(qc, shots=0, want_statevector=True).result().get_statevector()
)
print("amplitudes:", state.size, "| bytes:", state.nbytes)
assert state.size == 2**n
assert abs(np.linalg.norm(state) - 1.0) < 1e-12  # the state is normalised

Output ​

text
amplitudes: 1048576 | bytes: 16777216

The script checks that the state is normalized. On a CPU build this takes on the order of a second; on a CUDA-enabled build the planner moves it to the GPU (see CPU vs CUDA).

Scaling ​

QubitsState size
2016 MiB
25512 MiB
3016 GiB

Doubling the memory for each extra qubit is why dense simulation has a wall, and why other engines exist. See Memory Constraints.

Released under the Apache-2.0 license. GitHub · PyPI