inquanto.express
Express drivers
- class DriverGeneralizedHubbard(e=0.0, u=2.0, t=-1.0, v=0.0, n=2, ring=False)
Bases:
GeneralDriver
Creates Generalized Hubbard Hamiltonian with ring and chain topology.
- Parameters:
e (
Union
[float
,list
[float
]], default:0.0
) – on-site energies. If a float is given, applied to all sites. If list is given, its len must equal n, and index corresponds to site.u (
float
, default:2.0
) – U on-site repulsion.t (
float
, default:-1.0
) – Nearest neighbour coupling.v (
float
, default:0.0
) – Nearest neighbour coulomb repulsion.n (
int
, default:2
) – Number of sites.ring (
bool
, default:False
) – ring (True) or standalone chain (False).
- static generate_chain(n, diagonal, off_diagonal)
Generates nearest neighbour connectivity matrix for chain topology.
- generate_report(*args, **kwargs)
Generates report in a hierarchical dictionary format.
- static generate_ring(n, diagonal, off_diagonal)
Generates nearest neighbour connectivity matrix for ring topology.
- get_system()
Generates the Hubbard system.
- Returns:
Tuple
[FermionOperator
,FermionSpace
,FermionState
] – Hamiltonian fermion operator, Fock space, Hartree-Fock state.
- print_json_report(*args, **kwargs)
Prints report in json format.
- class DriverHubbardDimer(t=0.2, u=2.0)
Bases:
GeneralDriver
Driver creating a dimer Hubbard Hamiltonian.
- Parameters:
- generate_report(*args, **kwargs)
Generates report in a hierarchical dictionary format.
- get_system()
Generates the Hubbard dimer.
- Returns:
Tuple
[FermionOperator
,FermionSpace
,FermionState
] – Hamiltonian fermion operator, Fock space, Hartree-Fock state.
- print_json_report(*args, **kwargs)
Prints report in json format.
- class DriverIsingCustomConnectivity(j, h, connectivity_matrix)
Bases:
GeneralDriver
Driver for a transverse-field Ising hamiltonian with general lattice topology from a qubit connectivity matrix.
Builds the hamiltonian: \(H = \sum_{i<j}^{N_q} J_{ij} Z_i Z_j + h\sum_i^{N_q} X_i\), where \(N_q\) is the number of qubits, and \(J_{ij}\) is the product of the interaction strength
j
and the connectivity matrix.- Parameters:
- generate_report(*args, **kwargs)
Generates report in a hierarchical dictionary format.
- get_system()
Return the qubit hamiltonian, space and state.
- Returns:
Tuple
[QubitOperator
,QubitSpace
,QubitState
] – TFIM qubit hamiltonian, corresponding qubit space, qubit state of zeroes (uniform ferromagnetic state).
- print_json_report(*args, **kwargs)
Prints report in json format.
- class DriverIsing1D(j, h, n)
Bases:
DriverIsingCustomConnectivity
Driver for a 1D, nearest-neighbour transverse-field Ising hamiltonian with open boundary conditions.
Builds the hamiltonian: \(H = J\sum_{i}^{N_q} Z_i Z_{i+1} + h\sum_i^{N_q} X_i\), where \(N_q\) is the number of qubits.
- Parameters:
- generate_report(*args, **kwargs)
Generates report in a hierarchical dictionary format.
- get_system()
Return the qubit hamiltonian, space and state.
- Returns:
Tuple
[QubitOperator
,QubitSpace
,QubitState
] – TFIM qubit hamiltonian, corresponding qubit space, qubit state of zeroes (uniform ferromagnetic state).
- print_json_report(*args, **kwargs)
Prints report in json format.
- class DriverIsing1DRing(j, h, n)
Bases:
DriverIsingCustomConnectivity
Driver for a 1D, nearest-neighbour transverse-field Ising hamiltonian with periodic boundary conditions.
Builds the hamiltonian: \(H = J\sum_{i}^{N_q} Z_i Z_{i+1} + h\sum_i^{N_q} X_i\), where \(N_q\) is the number of qubits. The first and last qubits are connected to form a ring geometry.
- Parameters:
- generate_report(*args, **kwargs)
Generates report in a hierarchical dictionary format.
- get_system()
Return the qubit hamiltonian, space and state.
- Returns:
Tuple
[QubitOperator
,QubitSpace
,QubitState
] – TFIM qubit hamiltonian, corresponding qubit space, qubit state of zeroes (uniform ferromagnetic state).
- print_json_report(*args, **kwargs)
Prints report in json format.
Express functions
Data for example systems for testing.
- get_noisy_backend(n_qubits, cx_err=0.008, ro_err=0.01)
Makes a noisy Aer backend with specified error rates.
This function uses the AerBackend from pytket.extensions.qiskit and sets up a custom noise model with specified depolarizing error rates for CX gates and readout errors for qubits.
- Parameters:
- Returns:
Backend
– A noisy Aer backend instance constructed with the specified noise model.
Examples
>>> backend = get_noisy_backend(5) >>> type(backend) <class 'pytket.extensions.qiskit.backends.aer.AerBackend'>
- get_system(data_in)
Return the fermionic Hamiltonian operator, Fock space, and Hartree Fock state from an express dataset.
- Parameters:
data_in (
Union
[str
,dict
[str
,Any
],tuple
]) – An express dataset file name, or previously loaded express dataset.- Returns:
Tuple
[Union
[ChemistryRestrictedIntegralOperator
,ChemistryUnrestrictedIntegralOperator
],FermionSpace
,FermionState
] – Fermion Hamiltonian, Fock space, Fock state.
- list_h5()
Lists the predefined h5 data files in the express module.
- load_h5(name, as_tuple=False)
Loads predefined results from the express module.
- propagate(qubit_hamiltonian, qubit_state, t)
Propagate a qubit state with a Hamiltonion to a point of time.
- Parameters:
qubit_hamiltonian (
QubitOperator
) – A Hamiltonian qubit operator.qubit_state (
QubitState
) – The initial qubit state.
- Returns:
The evolved qubit state, \(|\Psi(t)\rangle = e^{-itH} |\Psi(0)\rangle\)
- random_circuit_ansatz(n_qubit=3, seed=0, return_sv=False)
Constructs a random circuit ansatz.
A random normalised complex statevector will be generated with a seed, and by using StatePreparationBox a circuit will be built.
- Parameters:
- Returns:
Union
[CircuitAnsatz
,Tuple
[CircuitAnsatz
,ndarray
[Any
,dtype
[TypeVar
(_ScalarType_co
, bound=generic
, covariant=True)]]]] – A circuit ansatz built with StatePreparationBox from pytket, and optionally the statevector.
- run_rhf(hamiltonian_operator, n_electron, guess_mo_coeff=None, maxit=MAXIT, conv=CONV)
Runs a simple RHF calculation for hamiltonian_operator.
Warning
It is not advised for production calculation, but for testing.
- Parameters:
hamiltonian_operator (
ChemistryRestrictedIntegralOperator
) – A Hamiltonian operator.n_electron (
int
) – number of electrons.guess_mo_coeff (
Optional
[ndarray
], default:None
) – initial guess for mo_coeff.maxit (
int
, default:MAXIT
) – Maximum number of iteration.conv (
float
, default:CONV
) – Criterion for convergence.
- Returns:
Total energy, orbital energies, mo orbitals, density matrix.
- run_rohf(hamiltonian_operator, n_electron, spin, guess_mo_coeff=None, maxit=MAXIT, conv=CONV)
Runs a simple ROHF calculation for hamiltonian_operator.
Warning
It is not advised for production calculation, but for testing.
- Parameters:
hamiltonian_operator (
ChemistryRestrictedIntegralOperator
) – A hamitlonian operator.n_electron (
int
) – Number of electrons.spin (
int
) – Spin.guess_mo_coeff (
Optional
[ndarray
], default:None
) – Initial guess for mo_coeff.maxit (
int
, default:MAXIT
) – Maximum number of iteration.conv (
float
, default:CONV
) – Criterion for convergence.
- Returns:
Total energy, orbital energies, mo orbitals, density matrix.
- run_time_evolution(initial_state, time_span, qubit_hamiltonian, *operators, n_qubits=None, real=True)
Simulating exact time evolution.
- Parameters:
initial_state (
QubitState
) – The initial qubit state for the time evolution.time_span (
Sequence
[Union
[float
,complex
]]) – The time span, which is a list of points of time.qubit_hamiltonian (
QubitOperator
) – The Hamiltonian to drive the time evolution.*operators (
QubitOperator
) – Optional operators for which the expectation value should be calculated during the evolution.real (
bool
, default:True
) – Optional to switch to imaginary time evolution. Default value is real.n_qubits (
int
, default:None
)
- Returns:
Tuple
[List
[QubitState
],...
] – The qubit states for each time in the time span and the expectation values.
- run_vqe(ansatz, hamiltonian, backend, with_gradient=True, minimizer=MinimizerScipy(method=OptimizationMethod.L_BFGS_B_smooth), initial_parameters=None)
Performs a black-box, state-vector-only variational quantum eigensolver simulation.
This is a wrapper over the instantiation of the AlgorithmVQE class and execution of its build() and run() methods. Most of the parameters have default values.
- Parameters:
ansatz (
GeneralAnsatz
) – an ansatz object.hamiltonian (
Union
[QubitOperator
,BaseChemistryIntegralOperator
]) – a Hamiltonian object.backend (
Backend
) – a backend object to perform calculation with.with_gradient (
bool
, default:True
) – whether to use objective function gradient in the VQE calculationminimizer (
GeneralMinimizer
, default:MinimizerScipy(method=OptimizationMethod.L_BFGS_B_smooth)
) – variational classical minimizer to perform the parameter search.initial_parameters (
Optional
[SymbolDict
], default:None
) – a set of initial Ansatz parameters. If not provided, defaulted to all zeros.
- Returns:
AlgorithmVQE
– AlgorithmVQE object after the AlgorithmVQE.build().run() method has been executed.
Examples
>>> from inquanto.express import load_h5 >>> from inquanto.states import FermionState >>> from inquanto.spaces import FermionSpace >>> from inquanto.ansatzes import FermionSpaceAnsatzUCCSD >>> from pytket.extensions.qiskit import AerStateBackend >>> hamiltonian = load_h5("h2_sto3g.h5", as_tuple=True).hamiltonian_operator.qubit_encode() >>> backend = AerStateBackend() >>> state = FermionState([1, 1, 0, 0]) >>> space = FermionSpace(4) >>> ansatz = FermionSpaceAnsatzUCCSD(fermion_space=space, fermion_state=state) >>> vqe = run_vqe(ansatz, hamiltonian, backend) # TIMER ... >>> print(round(vqe.final_value, 8)) -1.13684658
- save_h5_system(fname, ham, fermion_space, hf_state, **kwargs)
Save essential system information to the *.h5 file.
This function can be used both for molecular and periodic systems.
- Parameters:
fname (
str
) – h5 filename to save the system to.ham (
FermionOperator
) – fermionic hamiltonian for the system.fermion_space (
FermionSpace
) – fermion space object for the system.hf_state (
FermionState
) – fermionic reference state for the system.**kwargs – Additional data to be saved.