Heisenberg VQE example and shot-noise energy estimator.
This example constructs a Heisenberg-style Hamiltonian (ZZ/XX/YY couplings
plus local Z fields), builds an instance of the C++-backed
`qgd_Variational_Quantum_Eigensolver_Base` wrapper, applies a parameterized
ansatz, and demonstrates both exact (ideal) and shot-noise energy estimation.
The file shows how to format the term lists passed to the C++ wrapper:
`zz_terms`, `xx_terms`, `yy_terms` each contain items of the form
`{"i": int, "j": int, "coeff": float}` and `z_terms` contains
`{"i": int, "coeff": float}`. The shot-noise API expects a dictionary
containing these lists together with `shots`, `p_readout`, and `seed`.
This script is intended for interactive runs and manual inspection. For
lightweight automated checks that do not require compiling the native
extension, see `tests/VQE/test_shot_noise_measurement.py` which provides
fallback implementations and fast unit tests.
| def shot_noise_measurement.generate_zz_xx_hamiltonian |
( |
|
n_qubits, |
|
|
|
h = 0.5, |
|
|
|
topology = None, |
|
|
|
Jz = 1.0, |
|
|
|
Jx = 1.0, |
|
|
|
Jy = 1.0 |
|
) |
| |
Create a Heisenberg-like Hamiltonian and a Pauli-format oplist.
The Hamiltonian built is:
H = sum_{(i,j) in topology} [ Jz Z_i Z_j + Jx X_i X_j + Jy Y_i Y_j ]
+ sum_i h_i Z_i
Parameters
----------
n_qubits : int
Number of qubits in the system.
h : float or array-like, optional
Local Z field magnitude (scalar for uniform field or array-like for
per-qubit fields). Default 0.5.
topology : sequence of (i, j) pairs, optional
Edge list describing which qubit pairs are coupled. If ``None``, a
nearest-neighbour ring topology is used.
Jz, Jx, Jy : float, optional
Coupling strengths for the ZZ, XX and YY terms respectively.
Returns
-------
H_sparse : scipy.sparse.csr_matrix
The full Hamiltonian as a sparse matrix in the computational (Z)
basis. Suitable for small-system exact calculations.
oplist : list
A list of tuples describing Pauli terms in the form expected by the
wrapper and by :class:`qiskit.quantum_info.SparsePauliOp`:
("ZZ", [i, j], coeff), ("XX", [i, j], coeff), ("YY", [i, j], coeff),
and ("Z", [i], coeff).
Notes
-----
The returned ``oplist`` is used to build dictionaries passed to the C++
wrapper shot-noise routine (see ``zz_terms_for_dict`` etc. in
:func:`main`). The sparse Hamiltonian is convenient for exact energy
evaluation using ``scipy.sparse.linalg.eigsh`` for small systems.
Definition at line 38 of file shot_noise_measurement.py.