numpy_statevector
¶
Execute a circuit program with a NumPy statevector.
Qubit 0 is the least significant bit of the statevector index. Each gate is applied by moving those qubit axes next to each other and multiplying, which keeps the update readable on a small register. This is the reference evolution for small programs. Correctness and readability come before performance.
Reset, a measurement followed by a gate, and Prepare after the register
has left |0...0> are rejected.
statevector
¶
statevector(program: Program) -> NDArray[complex128]
Return the statevector of program before its terminal measurement.
The register starts in |0...0>. Prepare writes its amplitudes only
while the register is still in that state. Barrier is ignored. A
Measure may appear only after every gate.
Parameters:
-
program(Program) –Operations to apply.
Returns:
-
NDArray[complex128]–Complex amplitudes. Qubit
qis bitqof the index, so qubit 0 is -
NDArray[complex128]–the least significant bit.
Raises:
-
NotImplementedError–programresets a qubit, measures a qubit and then applies a gate, or prepares a state after the register has left|0...0>.
Source code in shadowsim/backends/numpy_statevector.py
expectations
¶
expectations(
program: Program, qubits: Sequence[int], pauli: _Pauli
) -> list[float]
Return the exact expectation of pauli on each qubit in qubits.
Parameters:
-
program(Program) –Circuit to evolve, before the computational-basis measurement.
-
qubits(Sequence[int]) –Qubit indexes. The result follows this order.
-
pauli(_Pauli) –"Z"or"X".
Returns:
-
list[float]–One expectation per entry of
qubits.
Raises:
-
ValueError–pauliis not"X"or"Z", or an index is outsideprogram. -
NotImplementedError–programuses an operation :func:statevectorcannot apply.
Source code in shadowsim/backends/numpy_statevector.py
sample_counts
¶
sample_counts(
program: Program, *, shots: int, seed: int | None
) -> dict[str, int]
Sample the computational-basis distribution of program.
A terminal Measure does not change that distribution. Keys use Qiskit
order: the rightmost character is qubit 0. The same seed returns the
same counts. None draws without a fixed seed.
Parameters:
-
program(Program) –Circuit to sample.
-
shots(int) –Number of shots. Must be a positive integer.
-
seed(int | None) –Seed for
numpy.random.Generator, orNone.
Returns:
-
dict[str, int]–Mapping from bitstring to shot count. Outcomes that never occurred are
-
dict[str, int]–omitted.
Raises:
-
ValueError–shotsis not a positive integer, or the statevector probabilities do not sum to 1. -
TypeError–seedis not anintorNone. -
NotImplementedError–programuses an operation :func:statevectorcannot apply.