Skip to content

Statevector ​

An exact statevector, compared with Qiskit's own reference simulator.

py
"""Exact statevectors, checked against Qiskit's own reference simulator."""

import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

from menos.qiskit import MENOSBackend

qc = QuantumCircuit(3)
qc.h(0)
qc.cx(0, 1)
qc.rx(0.7, 2)
qc.cp(0.4, 1, 2)
qc.t(0)

backend = MENOSBackend()
state = np.asarray(backend.run(qc, shots=0, want_statevector=True).result().get_statevector())
reference = Statevector(qc).data

print("amplitudes:", np.round(state, 6))
assert np.max(np.abs(state - reference)) < 1e-12
print("max |difference| from Qiskit's Statevector:", float(np.max(np.abs(state - reference))))

Output ​

text
amplitudes: [0.664237+0.j       0.      +0.j       0.      +0.j
 0.469686+0.469686j 0.      -0.242465j 0.      +0.j
 0.      +0.j       0.22468 -0.09115j ]
max |difference| from Qiskit's Statevector: 5.551115123125783e-17

The two results differ by about 6e-17, which is double-precision rounding. The script asserts the difference is below 1e-12.

See also ​

Statevectors · Conventions

Released under the Apache-2.0 license. GitHub · PyPI