TheAlgorithms/Python · error · ValueError
number of qubits too large to simulate(>10).
Error message
number of qubits too large to simulate(>10).
What it means
Raised by quantum_fourier_transform in quantum/q_fourier_transform.py when number_of_qubits exceeds 10. The function builds and executes a full-state Qiskit circuit with 10000 shots, so the qubit count is capped because the state vector grows as 2^n and simulation cost explodes beyond ~10 qubits. This is a hard precondition guard, not a Qiskit failure: the check fires before any circuit is constructed. Note the earlier guards already rejected strings, non-positive, and non-integer values, so this specific error means the value passed all of those but is simply too large.
Source
Thrown at quantum/q_fourier_transform.py:65
...
TypeError: number of qubits must be a integer.
>>> quantum_fourier_transform(100)
Traceback (most recent call last):
...
ValueError: number of qubits too large to simulate(>10).
>>> quantum_fourier_transform(0.5)
Traceback (most recent call last):
...
ValueError: number of qubits must be exact integer.
"""
if isinstance(number_of_qubits, str):
raise TypeError("number of qubits must be a integer.")
if number_of_qubits <= 0:
raise ValueError("number of qubits must be > 0.")
if math.floor(number_of_qubits) != number_of_qubits:
raise ValueError("number of qubits must be exact integer.")
if number_of_qubits > 10:
raise ValueError("number of qubits too large to simulate(>10).")
qr = QuantumRegister(number_of_qubits, "qr")
cr = ClassicalRegister(number_of_qubits, "cr")
quantum_circuit = QuantumCircuit(qr, cr)
counter = number_of_qubits
for i in range(counter):
quantum_circuit.h(number_of_qubits - i - 1)
counter -= 1
for j in range(counter):
quantum_circuit.cp(np.pi / 2 ** (counter - j), j, counter)
for k in range(number_of_qubits // 2):
quantum_circuit.swap(k, number_of_qubits - k - 1)
# measure all the qubitsView on GitHub (pinned to f5988cc097)
Solutions
- Reduce the qubit count to 10 or fewer, e.g. quantum_fourier_transform(10).
- If n comes from user input or config, clamp or validate it: n = min(n, 10) or assert 1 <= n <= 10 before calling.
- If you genuinely need > 10 qubits, run on real quantum hardware or a cloud simulator (e.g. IBM Quantum runtime) instead of this local Aer-based function.
- Fork the local function and remove/raise the cap only if you have the memory for a 2^n state vector (11 qubits is already 2048 amplitudes per shot).
Example fix
# before
result = quantum_fourier_transform(user_n) # user_n = 12 -> ValueError
# after
if user_n > 10:
raise SystemExit("QFT demo supports at most 10 qubits for local simulation")
result = quantum_fourier_transform(user_n) Defensive patterns
Strategy: validation
Validate before calling
def safe_qft(n):
if not isinstance(n, int) or isinstance(n, bool):
raise TypeError('number_of_qubits must be int')
if not 1 <= n <= 10:
raise ValueError('number_of_qubits must be between 1 and 10 for local simulation')
return quantum_fourier_transform(n) Type guard
def is_simulatable_qubit_count(n) -> bool:
return isinstance(n, int) and not isinstance(n, bool) and 1 <= n <= 10 Try / catch
try:
counts = quantum_fourier_transform(n)
except ValueError as e:
# covers >10, <=0, and non-integer messages; check str(e) if you need to distinguish
print(f'Skipping QFT: {e}') Prevention
- Clamp user-provided qubit counts to 1..10 before calling simulation code.
- Treat the cap as a memory limit: 2^n grows fast; do not fork-and-raise it on a laptop.
- Prefer real hardware or cloud simulators for registers above 10 qubits.
When it happens
Trigger: Calling quantum_fourier_transform(11), quantum_fourier_transform(100), or any integer-valued input greater than 10 (e.g. quantum_fourier_transform(10.0) passes the floor check and then 10.0 is fine, but 11.0 or 12 triggers it). Also triggered when number_of_qubits comes from a config value, CLI arg, or loop parameter that was never clamped.
Common situations: Porting textbook QFT examples (which often use small n) to larger registers; parameter sweeps that iterate n over a range including values > 10; copying Shor's algorithm or quantum phase estimation demos that assume many qubits; misunderstanding that the cap is about local simulator memory, not the algorithm itself.
Related errors
- number of qubits must be a integer.
- number of qubits must be > 0.
- k integer must be greater or equal to zero.
- All numbers must be positive
- Expected a_coeffs to have {self.order + 1} elements for {sel
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5c176fbbf28d1951.
Report an issue: GitHub.