Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Functions | Variables
tests.VQE.test_shot_noise_measurement Namespace Reference

Functions

def _kron_n (ops)
 
def _python_shot_noise_z_estimator (z_terms, shots, p_readout, seed=0)
 
def generate_hamiltonian (n)
 
def generate_hamiltonian_tmp (n)
 
def generate_zz_xx_hamiltonian (n_qubits, h=0.5, topology=None, Jz=1.0, Jx=1.0, Jy=1.0)
 
def is_hermitian (H, tol=1e-10)
 
def matrices_differ (A, B, tol=1e-12)
 
def test_generate_hamiltonian_per_qubit_h ()
 
def test_generate_hamiltonian_returns_sparse_or_ndarray ()
 
def test_generate_zz_xx_hamiltonian_parameter_dependence ()
 
def test_hamiltonian_shape_and_hermitian (n)
 
def test_hamiltonian_spectrum_real (n)
 
def test_oplist_indices_in_range ()
 
def test_wrapper_shot_noise_against_analytic ()
 
def test_z_only_shot_noise_analytic_and_simulation ()
 
def to_dense_if_small (H, max_dim=1<< 6)
 

Variables

 _I
 
 _X
 
 _Y
 
 _Z
 
 dtype
 
 generate_zz_xx_hamiltonian
 
 H
 
 heis_mod
 
 heis_path
 
 n
 
 repo_root
 
 spec
 
 state
 

Detailed Description

Unit tests for Hamiltonian helpers and shot-noise example plumbing.

This module contains small, fast tests intended for CI that validate the
construction and basic properties of Heisenberg-style Hamiltonians used by
the VQE examples. Tests are written to be robust when the native C++
extension is not available: the test runner first attempts to import the
real implementations from ``examples/VQE/Heisenberg_VQE.py`` and falls back
to lightweight Python-only generators if the import fails. This keeps CI
fast while still exercising the core logic.

Checks performed include:
- Hamiltonian shape and Hermiticity for small systems
- Eigenvalue reality for small dense matrices
- Sensitivity of the generator to coupling parameters (Jx/Jz/Jy)
- API contract for ``generate_hamiltonian`` (returns object with ``.shape``)

Function Documentation

◆ _kron_n()

def tests.VQE.test_shot_noise_measurement._kron_n (   ops)
private

Definition at line 68 of file test_shot_noise_measurement.py.

Here is the caller graph for this function:

◆ _python_shot_noise_z_estimator()

def tests.VQE.test_shot_noise_measurement._python_shot_noise_z_estimator (   z_terms,
  shots,
  p_readout,
  seed = 0 
)
private
Simple python Monte-Carlo estimator for Z-only Hamiltonians on the
all-|0> state. Assumes measurements are independent and readout flips
each measurement bit with probability `p_readout`.
Returns (mean_estimate, sample_variance_of_estimates).

Definition at line 242 of file test_shot_noise_measurement.py.

Here is the caller graph for this function:

◆ generate_hamiltonian()

def tests.VQE.test_shot_noise_measurement.generate_hamiltonian (   n)

Definition at line 33 of file test_shot_noise_measurement.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate_hamiltonian_tmp()

def tests.VQE.test_shot_noise_measurement.generate_hamiltonian_tmp (   n)

Definition at line 29 of file test_shot_noise_measurement.py.

Here is the caller graph for this function:

◆ generate_zz_xx_hamiltonian()

def tests.VQE.test_shot_noise_measurement.generate_zz_xx_hamiltonian (   n_qubits,
  h = 0.5,
  topology = None,
  Jz = 1.0,
  Jx = 1.0,
  Jy = 1.0 
)

Definition at line 79 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ is_hermitian()

def tests.VQE.test_shot_noise_measurement.is_hermitian (   H,
  tol = 1e-10 
)
Check Hermiticity (H == H^†) up to `tol`.

Returns (is_hermitian_bool, max_abs_deviation) so tests can give
informative failures.

Definition at line 132 of file test_shot_noise_measurement.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ matrices_differ()

def tests.VQE.test_shot_noise_measurement.matrices_differ (   A,
  B,
  tol = 1e-12 
)
Return True if matrices A and B differ by more than `tol`.

Accepts either sparse or dense inputs; for sparse results the
function compares stored values, otherwise `np.allclose` is used.

Definition at line 179 of file test_shot_noise_measurement.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_generate_hamiltonian_per_qubit_h()

def tests.VQE.test_shot_noise_measurement.test_generate_hamiltonian_per_qubit_h ( )
Pass an array for `h` and verify per-qubit fields are represented.

Definition at line 231 of file test_shot_noise_measurement.py.

◆ test_generate_hamiltonian_returns_sparse_or_ndarray()

def tests.VQE.test_shot_noise_measurement.test_generate_hamiltonian_returns_sparse_or_ndarray ( )
Confirm `generate_hamiltonian` returns either a dense ndarray or a
scipy sparse matrix with the expected 2^n shape (API contract test).

Definition at line 207 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ test_generate_zz_xx_hamiltonian_parameter_dependence()

def tests.VQE.test_shot_noise_measurement.test_generate_zz_xx_hamiltonian_parameter_dependence ( )
Ensure that changing coupling parameters (e.g. Jx) modifies the
produced Hamiltonian. This guards against parameter-ignored bugs.

Definition at line 197 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ test_hamiltonian_shape_and_hermitian()

def tests.VQE.test_shot_noise_measurement.test_hamiltonian_shape_and_hermitian (   n)
Verify generated Hamiltonian has the expected shape (2^n x 2^n)
and is Hermitian up to numerical tolerance. This is a basic
correctness check for the Hamiltonian generator.

Definition at line 151 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ test_hamiltonian_spectrum_real()

def tests.VQE.test_shot_noise_measurement.test_hamiltonian_spectrum_real (   n)
Check that eigenvalues are numerically real for small systems.

Hermitian matrices must have real eigenvalues; this catches subtle
bugs in matrix construction where non-Hermitian entries creep in.

Definition at line 164 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ test_oplist_indices_in_range()

def tests.VQE.test_shot_noise_measurement.test_oplist_indices_in_range ( )
All indices in the produced `oplist` must be within [0, n_qubits-1].

Definition at line 219 of file test_shot_noise_measurement.py.

◆ test_wrapper_shot_noise_against_analytic()

def tests.VQE.test_shot_noise_measurement.test_wrapper_shot_noise_against_analytic ( )
If the C++ wrapper is importable, run a small-shot comparison of its
shot-noise estimator against the analytic Z-only result. Skips when the
native extension is not present.

Definition at line 299 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ test_z_only_shot_noise_analytic_and_simulation()

def tests.VQE.test_shot_noise_measurement.test_z_only_shot_noise_analytic_and_simulation ( )
Verify analytic formulas for Z-only Hamiltonians against Monte-Carlo
simulation (Python implementation). This does not require the C++
extension and tests statistical behavior of the estimator.

Definition at line 272 of file test_shot_noise_measurement.py.

Here is the call graph for this function:

◆ to_dense_if_small()

def tests.VQE.test_shot_noise_measurement.to_dense_if_small (   H,
  max_dim = 1 << 6 
)
Return a dense array for small sparse matrices, otherwise return
the original sparse object. This keeps tests fast while allowing
checks that require dense operations for small sizes.

Definition at line 119 of file test_shot_noise_measurement.py.

Here is the caller graph for this function:

Variable Documentation

◆ _I

tests.VQE.test_shot_noise_measurement._I
private

Definition at line 74 of file test_shot_noise_measurement.py.

◆ _X

tests.VQE.test_shot_noise_measurement._X
private

Definition at line 75 of file test_shot_noise_measurement.py.

◆ _Y

tests.VQE.test_shot_noise_measurement._Y
private

Definition at line 76 of file test_shot_noise_measurement.py.

◆ _Z

tests.VQE.test_shot_noise_measurement._Z
private

Definition at line 77 of file test_shot_noise_measurement.py.

◆ dtype

tests.VQE.test_shot_noise_measurement.dtype

Definition at line 74 of file test_shot_noise_measurement.py.

◆ generate_zz_xx_hamiltonian

tests.VQE.test_shot_noise_measurement.generate_zz_xx_hamiltonian

Definition at line 52 of file test_shot_noise_measurement.py.

◆ H

tests.VQE.test_shot_noise_measurement.H

Definition at line 315 of file test_shot_noise_measurement.py.

◆ heis_mod

tests.VQE.test_shot_noise_measurement.heis_mod

Definition at line 45 of file test_shot_noise_measurement.py.

◆ heis_path

tests.VQE.test_shot_noise_measurement.heis_path

Definition at line 41 of file test_shot_noise_measurement.py.

◆ n

tests.VQE.test_shot_noise_measurement.n

Definition at line 316 of file test_shot_noise_measurement.py.

◆ repo_root

tests.VQE.test_shot_noise_measurement.repo_root

Definition at line 39 of file test_shot_noise_measurement.py.

◆ spec

tests.VQE.test_shot_noise_measurement.spec

Definition at line 44 of file test_shot_noise_measurement.py.

◆ state

tests.VQE.test_shot_noise_measurement.state

Definition at line 317 of file test_shot_noise_measurement.py.