Ansatz

The Ansatz class in QDK/Chemistry represents a quantum chemical ansatz that combines a Hamiltonian operator with a wavefunction. This pairing enables energy expectation value calculations and forms the foundation for various quantum chemistry methods.

Overview

An ansatz in quantum chemistry represents a trial wavefunction paired with the system’s Hamiltonian operator. The Ansatz class encapsulates this fundamental concept by combining:

The class ensures consistency between the Hamiltonian and wavefunction components and provides methods for energy calculations and data access.

Properties

The Ansatz class provides access to:

  • Hamiltonian: The energy operator for the molecular system

  • Wavefunction: The quantum state description

  • Orbitals: Molecular orbital basis set (derived from Hamiltonian)

  • Energy calculation: Expectation value ⟨ψ|H|ψ⟩

  • Validation: Consistency checks between components

Creating an ansatz

An Ansatz object can be created from existing Hamiltonian and Wavefunction objects:

#include <iostream>
#include <qdk/chemistry.hpp>
#include <string>
using namespace qdk::chemistry::data;
using namespace qdk::chemistry::algorithms;

int main() {
  // Load H2 structure from XYZ file
  auto structure = Structure::from_xyz_file("../data/h2.structure.xyz");

  // SCF
  auto scf_solver = ScfSolverFactory::create();
  auto [E_scf, wfn_scf] = scf_solver->run(structure, 0, 1, "sto-3g");

  // Create Hamiltonian from SCF Orbitals
  auto ham_constructor = HamiltonianConstructorFactory::create();
  auto hamiltonian = ham_constructor->run(wfn_scf->get_orbitals());

  // Create Ansatz object from Hamiltonian and Wavefunction
  Ansatz ansatz(hamiltonian, wfn_scf);
from pathlib import Path
from qdk_chemistry.data import Ansatz, Structure
from qdk_chemistry.algorithms import create

# Load H2 molecule structure from XYZ file
structure = Structure.from_xyz_file(Path(__file__).parent / "../data/h2.structure.xyz")

# SCF
scf_solver = create("scf_solver")
E_scf, wfn_scf = scf_solver.run(
    structure, charge=0, spin_multiplicity=1, basis_or_guess="sto-3g"
)

# Create Hamiltonian from SCF orbitals
hamiltonian_constructor = create("hamiltonian_constructor")
hamiltonian = hamiltonian_constructor.run(wfn_scf.get_orbitals())

# Create ansatz
ansatz_scf = Ansatz(hamiltonian, wfn_scf)

Accessing ansatz data

The Ansatz class provides methods to access its components and perform calculations:

  // Access Ansatz components
  auto hamiltonian_ref = ansatz.get_hamiltonian();
  auto wavefunction_ref = ansatz.get_wavefunction();
  auto orbitals_ref = ansatz.get_orbitals();

  // Check component availability
  bool has_hamiltonian = ansatz.has_hamiltonian();
  bool has_wavefunction = ansatz.has_wavefunction();
  bool has_orbitals = ansatz.has_orbitals();

  // Calculate energy expectation value
  double energy = ansatz.calculate_energy();
  std::cout << "Energy expectation value: " << energy << " Ha" << std::endl;

  // Get summary
  std::string summary = ansatz.get_summary();
  std::cout << "Ansatz summary: " << summary << std::endl;
# Access ansatz components
hamiltonian_ref = ansatz_scf.get_hamiltonian()
wavefunction_ref = ansatz_scf.get_wavefunction()
orbitals_ref = ansatz_scf.get_orbitals()

# Check component availability
has_hamiltonian = ansatz_scf.has_hamiltonian()
has_wavefunction = ansatz_scf.has_wavefunction()
has_orbitals = ansatz_scf.has_orbitals()

# Calculate energy expectation value
energy = ansatz_scf.calculate_energy()
print(f"Energy expectation value: {energy:.8f} Hartree")

# Get summary information
summary = ansatz_scf.get_summary()
print(f"Ansatz summary: {summary}")

Further reading