qpandalite.algorithmics package#
Subpackages#
- qpandalite.algorithmics.ansatz package
- qpandalite.algorithmics.state_preparation package
- Submodules
- qpandalite.algorithmics.state_preparation.basis_state module
- qpandalite.algorithmics.state_preparation.dicke_state module
- qpandalite.algorithmics.state_preparation.hadamard_superposition module
- qpandalite.algorithmics.state_preparation.rotation_prepare module
- qpandalite.algorithmics.state_preparation.thermal_state module
- Module contents
Submodules#
qpandalite.algorithmics.measurement module#
Measurement module for quantum state characterization.
- qpandalite.algorithmics.measurement.basis_rotation_measurement(circuit, qubits=None, basis=None, shots=None)[源代码]
Measure a circuit by applying basis-rotation gates and then measuring in the computational (Z) basis.
For each qubit, the rotation applied before measurement is determined by the corresponding entry in
basis:``"Z"``: no rotation (Z basis, default)
``"X"``: Hadamard gate (H) → measures X basis
``"Y"``:
S^dagger H→ measures Y basis``"I"``: no rotation (Z basis, identity)
When
shotsisNone, the statevector simulator is used to return the exact probability distribution. Whenshotsis given, the distribution is estimated from that many samples.- 参数:
circuit (Circuit) -- Quantum circuit (must contain MEASURE instructions).
qubits (List[int] | None) -- Indices of qubits to include.
Nonemeans all qubits.basis (str | List[str] | None) --
Per-qubit measurement basis. Can be:
A single string such as
"XYZ"(applied left-to-right toqubits), where each character is"I","X","Y", or"Z".A list of strings such as
["X", "Y", "Z"].None(default), which means all qubits use the Z basis.
shots (int | None) -- Number of measurement shots.
Nonereturns the exact probability vector from the statevector simulator.
- 返回:
- a
dictmapping each computational-basis outcome string (e.g.
"01") to its probability.
If
shotsis given: adictmapping outcome strings to integer counts (frequency).
- a
- 返回类型:
If
shotsisNone
- 抛出:
ValueError --
len(basis)does not matchlen(qubits).ValueError --
shotsis not a positive integer.ValueError --
basiscontains invalid characters.
示例
>>> from qpandalite.circuit_builder import Circuit >>> from qpandalite.algorithmics.measurement import basis_rotation_measurement >>> c = Circuit() >>> c.h(0) # |0⟩ → (|0⟩+|1⟩)/√2 >>> c.cx(0, 1) # Bell state (|00⟩+|11⟩)/√2 >>> c.measure(0, 1) >>> # Measure qubit 0 in X basis, qubit 1 in Z basis >>> probs = basis_rotation_measurement(c, basis="XZ") >>> abs(probs["00"] - 0.5) < 1e-6 # P(0) in X basis for |+⟩ is 0.5 True
- qpandalite.algorithmics.measurement.classical_shadow(circuit, qubits=None, shots=4096, n_shadow=None)[源代码]
Generate classical-shadow snapshots of a quantum state.
Each snapshot is obtained by:
For each qubit, choosing uniformly at random one of the three single-qubit Cliffords {I, H, S·H} — corresponding to measurement in the Z, X, or Y basis.
Injecting the corresponding gates before the existing MEASURE instructions in the circuit QASM.
Simulating the modified circuit once and recording the computational-basis outcomes.
Storing (unitary_indices, outcomes) as one snapshot.
The collection of snapshots enables estimating the expectation value of any Pauli string via
shadow_expectation()with sample complexity \(O(\log M / ε^2)\) for M observables.- 参数:
circuit (Circuit) -- Quantum circuit (must already contain MEASURE instructions).
qubits (List[int] | None) -- Indices of qubits to include.
Nonemeans all qubits used by the circuit.shots (int) -- Number of simulated measurement shots per snapshot. Higher shots reduce per-snapshot variance but are slower.
n_shadow (int | None) -- Number of independent shadow snapshots.
Noneauto-computes as2 * n * log(2/δ)withδ = 0.01. This bound guarantees fidelity error ≤ ε with high probability for up to M = exp(ε² n / 10) observables.
- 返回:
List of
ShadowSnapshotobjects.- 抛出:
ValueError --
shotsorn_shadowis not a positive integer.- 返回类型:
List[ShadowSnapshot]
示例
>>> from qpandalite.circuit_builder import Circuit >>> from qpandalite.algorithmics.measurement import ( ... classical_shadow, shadow_expectation ... ) >>> c = Circuit() >>> c.h(0) >>> c.cx(0, 1) >>> c.measure(0, 1) >>> shadows = classical_shadow(c, shots=1024, n_shadow=32) >>> est_ZZ = shadow_expectation(shadows, "ZZ") >>> abs(est_ZZ - 1.0) < 0.1 True
- qpandalite.algorithmics.measurement.pauli_expectation(circuit, pauli_string, shots=None)[源代码]
Measure the expectation value of a Pauli string on a circuit.
For each qubit i, the measurement basis is determined by
pauli_string[i]:'I': trace out (identity, contributes trivially)'Z': measure in the computational (Z) basis — no rotation needed'X': apply Hadamard before Z measurement'Y': apply Sdag then Hadamard before Z measurement
When
shotsisNone, the statevector simulator is used to compute the exact expectation analytically. Whenshotsis given, the circuit is simulatedshotstimes and the empirical frequency is used.- 参数:
circuit (Circuit) -- Quantum circuit. Must contain only gates supported by
QASM_Simulatorand end with measurement instructions.pauli_string (str) -- Case-insensitive Pauli string (e.g.
"XYZ","IZI"). Characters must beI,X,Y, orZ.shots (int | None) -- Number of measurement shots.
Noneuses statevector mode for the exact analytical value.
- 返回:
Expectation value ⟨psi|P|psi⟩ as a float in the interval
[-1, 1].- 抛出:
ValueError --
pauli_stringcontains invalid characters or its length does not match the number of qubits incircuit.ValueError --
shotsis not a positive integer.
- 返回类型:
float
示例
>>> from qpandalite.circuit_builder import Circuit >>> from qpandalite.algorithmics.measurement import pauli_expectation >>> c = Circuit() >>> c.h(0) >>> c.cx(0, 1) # Bell state (|00⟩+|11⟩)/√2 >>> c.measure(0, 1) >>> pauli_expectation(c, "ZZ", shots=None) # exact: 1.0 1.0 >>> abs(pauli_expectation(c, "ZZ", shots=10000) - 1.0) < 0.1 True
- qpandalite.algorithmics.measurement.shadow_expectation(shadows, pauli_string)[源代码]
Estimate the expectation value of a Pauli string from classical-shadow snapshots.
Computes the mean of single-snapshot HKP estimators. For a single observable the mean is optimal; median-of-means buys robustness only when estimating many Paulis with uniform tail-bound guarantees.
For each snapshot the single-qubit estimator is (Huang-Kueng-Preskill, single-qubit Clifford shadow inverse channel
M^{-1}(X) = 3X - I):s_i = 1 if Pauli_i = I s_i = 3 * (-1)^outcome_i if Pauli_i ≠ I and aligned with measured basis s_i = 0 if Pauli_i ≠ I and misaligned with measured basis
The n-qubit estimator is the product \(\hat{P}=\prod_i s_i\).
- 参数:
shadows (List[ShadowSnapshot]) -- List of
ShadowSnapshotfromclassical_shadow().pauli_string (str) -- Case-insensitive Pauli string (e.g.
"XYZ","IZI").
- 返回:
Estimated expectation value
<P>.- 抛出:
ValueError --
pauli_stringlength does not match snapshot size.ValueError --
pauli_stringcontains invalid characters.
- 返回类型:
float
示例
>>> shadows = classical_shadow(circuit, shots=1024, n_shadow=32) >>> shadow_expectation(shadows, "ZZ") # estimate <ZZ>
- qpandalite.algorithmics.measurement.state_tomography(circuit, qubits=None, shots=8192)[源代码]
Reconstruct the density matrix of a quantum state via complete tomography.
The method measures the circuit in all 3^n combinations of the single-qubit bases (X, Y, Z), then uses linear inversion over the Pauli basis to reconstruct the
d × ddensity matrix, whered = 2^nandnis the number of qubits being tomographied.- 参数:
circuit (Circuit) -- Quantum circuit (must already contain MEASURE instructions).
qubits (List[int] | None) -- Indices of qubits to include in the tomography.
Nonemeans all qubits used by the circuit, in their natural order.shots (int) -- Number of measurement shots per basis setting. Higher shots reduce statistical noise in the reconstruction.
- 返回:
A
(d, d)NumPy complex array representing the reconstructed density matrixρ, whered = 2**len(qubits). The matrix is always Hermitian (ρ = ρ†) and normalised (Tr(ρ) = 1).- 抛出:
ValueError --
shotsis not a positive integer.ValueError --
len(qubits)is zero or exceeds the circuit qubit count.
- 返回类型:
ndarray
示例
>>> from qpandalite.circuit_builder import Circuit >>> from qpandalite.algorithmics.measurement import state_tomography >>> c = Circuit() >>> c.h(0) # |0⟩ → (|0⟩+|1⟩)/√2 >>> c.cx(0, 1) # Bell state (|00⟩+|11⟩)/√2 >>> c.measure(0, 1) >>> rho = state_tomography(c, shots=4096) >>> rho.shape (4, 4) >>> abs(rho[0, 0]) # ≈ 0.5 (population of |00⟩) 0.5
- qpandalite.algorithmics.measurement.tomography_summary(rho, label='ρ', reference_state=None)[源代码]
Print a human-readable summary of a density matrix tomography result.
Shows eigenvalues, purity (:math:` mathrm{Tr}( rho^2)`), trace, and, if
reference_stateis provided, the fidelity \(F( rho, \sigma) = ( mathrm{Tr}sqrt{sqrt{ rho}\sigmasqrt{ rho}})^2\).- 参数:
rho (ndarray) -- Density matrix from
state_tomography().label (str) -- Label to print alongside the matrix (e.g.
"ρ").reference_state (ndarray | None) -- Optional reference density matrix for fidelity computation. If
Nonethe fidelity is skipped.
- 返回类型:
None
示例
>>> from qpandalite.circuit_builder import Circuit >>> from qpandalite.algorithmics.measurement import ( ... state_tomography, tomography_summary ... ) >>> c = Circuit() >>> c.h(0) >>> c.cx(0, 1) >>> c.measure(0, 1) >>> rho = state_tomography(c, shots=4096) >>> tomography_summary(rho)
Module contents#
Algorithmics module — reusable quantum algorithm components.