Skip to content

Commit

Permalink
Move initialization of kernel params out of hamlib, put into kernel_draw
Browse files Browse the repository at this point in the history
Fix minor issue with printing of inverse circuit for method 3
  • Loading branch information
rtvuser1 committed Jul 28, 2024
1 parent 23aa35b commit ec40da0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
11 changes: 5 additions & 6 deletions hamlib/qiskit/hamlib_simulation_benchmark.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'''
Hamiltonian Simulation Benchmark Program - Qiskit
(C) Quantum Economic Development Consortium (QED-C) 2024.
'''
'''
This program benchmarks Hamiltonian simulation using Qiskit.
The central function is the `run()` method, which orchestrates the entire benchmarking process.
HamiltonianSimulation forms the trotterized circuit used in the benchmark.
HamiltonianSimulationExact runs a classical calculation that perfectly simulates hamiltonian evolution, although it does not scale well.
HamiltonianSimulationExact runs a classical calculation that
perfectly simulates hamiltonian evolution, although it does not scale well.
'''

import json
Expand All @@ -27,7 +26,7 @@

import hamlib_simulation_kernel
from hamlib_simulation_kernel import HamiltonianSimulation, kernel_draw, get_valid_qubits
from hamlib_utils import create_full_filenames, construct_dataset_name, set_default_parameter_values
from hamlib_utils import create_full_filenames, construct_dataset_name
from hamiltonian_simulation_exact import HamiltonianSimulationExact, HamiltonianSimulationExact_Noiseless


Expand Down Expand Up @@ -121,7 +120,7 @@ def analyze_and_print_result(qc, result, num_qubits: int,
ts = time.time()
correct_dist = HamiltonianSimulationExact_Noiseless(n_spins=num_qubits,init_state=init_state)
if verbose:
print(f"... noiseless simulation for expected distribution time = {round((time.time() - ts), 3)} sec")
print(f"... noiseless simulation for expected distribution time = {round((time.time() - ts), 3)} sec")

elif method == 2:
if verbose:
Expand Down Expand Up @@ -241,7 +240,7 @@ def run(min_qubits: int = 2, max_qubits: int = 8, max_circuits: int = 1,
return

# Set default parameter values for the hamiltonians
set_default_parameter_values(hamlib_simulation_kernel.filename)
hamlib_simulation_kernel.set_default_parameter_values(hamlib_simulation_kernel.filename)

# assume default init_state if not given
if init_state == None:
Expand Down
57 changes: 52 additions & 5 deletions hamlib/qiskit/hamlib_simulation_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,55 @@

from hamlib_utils import process_hamiltonian_file, needs_normalization, normalize_data_format, parse_hamiltonian_to_sparsepauliop, determine_qubit_count

def set_default_parameter_values(filename):
"""
Set defaults for the parameters that are relevant to the specific Hamiltonian
"""

global global_U, global_enc, global_ratio, global_rinst, global_h, global_pbc_val

if filename == 'tfim.hdf5' or filename == 'heis.hdf5':
if global_h == None:
global_h = 0.1
if global_pbc_val == None:
global_pbc_val = 'nonpbc'
global_U = None
global_enc = None
global_ratio = None
global_rinst = None
elif filename == 'random_max3sat-hams.hdf5':
if global_ratio == None:
global_ratio = 2
if global_rinst == None:
global_rinst = '00'
global_U = None
global_enc = None
global_h = None
global_pbc_val = None
elif filename == 'FH_D-1.hdf5':
if global_U == None:
global_U = 0
if global_enc == None:
global_enc = 'bk'
if global_pbc_val == None:
global_pbc_val = 'nonpbc'
global_ratio = None
global_rinst = None
global_h = None
elif filename == 'BH_D-1_d-4.hdf5':
if global_U == None:
global_U = 2
if global_enc == None:
global_enc = 'gray'
if global_pbc_val == None:
global_pbc_val = 'nonpbc'
global_ratio = None
global_rinst = None
global_h = None
else:
print("No such hamiltonian is available.")


def process_data(data):
"""
Process the given data to construct a Hamiltonian in the form of a SparsePauliOp and determine the number of qubits.
Expand Down Expand Up @@ -232,9 +281,11 @@ def create_circuit(n_spins: int, time: float = 1, num_trotter_steps: int = 5, me
# Append K Trotter steps of inverse, if method 3
inv = None
if method == 3:
INV_ = inv = evo.inverse()
inv = evo.inverse()
inv.name = "e^iHt"
circuit = create_trotter_steps(num_trotter_steps, inv, operator, circuit)
if n_spins <= 6:
INV_ = inv

circuit.measure_all()
return circuit, hamiltonian, evo
Expand Down Expand Up @@ -346,16 +397,12 @@ def kernel_draw(hamiltonian: str = "hamlib", use_XX_YY_ZZ_gates: bool = False, m
# create a small circuit, just to display this inverse evolution subcircuit structure
if INV_ is not None:
print(" Inverse Evolution Operator (e^iHt) = Inverse of Above Circuit")
''' DEVNOTE: This fails on some systems with an error about mismatch of Q and C widths
'''
try:
qctt = QuantumCircuit(QC_.num_qubits)
qctt.append(INV_, range(QC_.num_qubits))
print(transpile(qctt, optimization_level=3))
except:
print(f" WARNING: cannot display inverse circuit.")


else:
print(" ... circuit too large!")

Expand Down
43 changes: 0 additions & 43 deletions hamlib/qiskit/hamlib_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import requests
import zipfile
import json
import hamlib_simulation_kernel

verbose = False

Expand Down Expand Up @@ -42,48 +41,6 @@ def create_full_filenames(hamiltonian_name):
else:
return hamiltonian_name + '.hdf5'

def set_default_parameter_values(filename):
if filename == 'tfim.hdf5' or filename == 'heis.hdf5':
if hamlib_simulation_kernel.global_h == None:
hamlib_simulation_kernel.global_h = 0.1
if hamlib_simulation_kernel.global_pbc_val == None:
hamlib_simulation_kernel.global_pbc_val = 'nonpbc'
hamlib_simulation_kernel.global_U = None
hamlib_simulation_kernel.global_enc = None
hamlib_simulation_kernel.global_ratio = None
hamlib_simulation_kernel.global_rinst = None
elif filename == 'random_max3sat-hams.hdf5':
if hamlib_simulation_kernel.global_ratio == None:
hamlib_simulation_kernel.global_ratio = 2
if hamlib_simulation_kernel.global_rinst == None:
hamlib_simulation_kernel.global_rinst = '00'
hamlib_simulation_kernel.global_U = None
hamlib_simulation_kernel.global_enc = None
hamlib_simulation_kernel.global_h = None
hamlib_simulation_kernel.global_pbc_val = None
elif filename == 'FH_D-1.hdf5':
if hamlib_simulation_kernel.global_U == None:
hamlib_simulation_kernel.global_U = 0
if hamlib_simulation_kernel.global_enc == None:
hamlib_simulation_kernel.global_enc = 'bk'
if hamlib_simulation_kernel.global_pbc_val == None:
hamlib_simulation_kernel.global_pbc_val = 'nonpbc'
hamlib_simulation_kernel.global_ratio = None
hamlib_simulation_kernel.global_rinst = None
hamlib_simulation_kernel.global_h = None
elif filename == 'BH_D-1_d-4.hdf5':
if hamlib_simulation_kernel.global_U == None:
hamlib_simulation_kernel.global_U = 2
if hamlib_simulation_kernel.global_enc == None:
hamlib_simulation_kernel.global_enc = 'gray'
if hamlib_simulation_kernel.global_pbc_val == None:
hamlib_simulation_kernel.global_pbc_val = 'nonpbc'
hamlib_simulation_kernel.global_ratio = None
hamlib_simulation_kernel.global_rinst = None
hamlib_simulation_kernel.global_h = None
else:
print("No such hamiltonian is available.")

def extract_dataset_hdf5(filename, dataset_name):
"""
Extract a dataset from an HDF5 file.
Expand Down

0 comments on commit ec40da0

Please sign in to comment.