Skip to content

Commit

Permalink
Fix issue with printing of ham sim circuits
Browse files Browse the repository at this point in the history
saved circuit needs to be cleared after printing, so it doesn't incorrectly draw if hamiltonian class changed
  • Loading branch information
rtvuser1 committed Jul 4, 2024
1 parent 56e3204 commit 64a5163
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ def execution_handler(qc, result, num_qubits, type, num_shots):

# Submit circuit for execution on target (simulator, cloud simulator, or hardware)
ex.submit_circuit(qc, num_qubits, circuit_id, num_shots)

# draw a sample circuit
if circuit_id == 0:
qc_object.kernel_draw()

# Wait for some active circuits to complete; report metrics when groups complete
ex.throttle_execution(metrics.finalize_group)
Expand All @@ -235,7 +231,6 @@ def execution_handler(qc, result, num_qubits, type, num_shots):
ex.finalize_execution(metrics.finalize_group)

##########

# draw a sample circuit
qc_object.kernel_draw()

Expand Down
58 changes: 30 additions & 28 deletions hamiltonian-simulation/qiskit/hamiltonian_simulation_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
## use initial state in the abstract class
class HamiltonianKernel(object):

MAX_PRINT_SIZE = 6 # default maximum size to print circuit
# class variables saved for printing
MAX_PRINT_SIZE = 6 # default maximum size to print circuit
QCI_ = None # Initial Circuit
QC_ = None # Total Circuit
QCH_ = None # Hamiltonian
QC2D_ = None # Mirror Circuit
QCRS_ = None # Resultant Pauli

def __init__(self, n_spins, K, t, hamiltonian, w, hx, hz, use_XX_YY_ZZ_gates, method, random_pauli_flag, init_state):
self.n_spins = n_spins
Expand All @@ -50,24 +56,22 @@ def __init__(self, n_spins, K, t, hamiltonian, w, hx, hz, use_XX_YY_ZZ_gates, me
self.random_pauli_flag = random_pauli_flag
self.method = method
self.init_state = init_state


self.QCI_ = None # Initial Circuit
self.QC_ = None # Total Circuit
self.QCH_ = None # Hamiltonian
self.QC2D_ = None # Mirror Circuit
self.QCRS_ = None # Resultant Pauli

self.qr = QuantumRegister(n_spins)
self.cr = ClassicalRegister(n_spins)
self.qc = QuantumCircuit(self.qr, self.cr, name = hamiltonian)

def overall_circuit(self):
i_state = self.initial_state() #create initial state
self.qc.append(i_state, self.qr) #append the initial state to the quantum circuit
circuit = self.create_hamiltonian() #create the hamiltonian circuit
self.qc.append(circuit, self.qr) #append the hamiltoniain to the quantum circuit

# create initial state and append to the overall circuit
i_state = self.initial_state()
self.qc.append(i_state, self.qr)

# create Hamiltonian evolution circuit and append to the overall circuit
hamiltonian_circuit = self.create_hamiltonian()
self.qc.append(hamiltonian_circuit, self.qr)

# if mirrored, create the inverse circuit and append to overall circuit
inverse_circuit = None
if self.method == 3:
#checks if random pauli flag is true to apply quasi inverse.
Expand All @@ -83,14 +87,12 @@ def overall_circuit(self):
for i_qubit in range(self.n_spins):
self.qc.measure(self.qr[i_qubit], self.cr[i_qubit])

# Save circuits and subcircuits for possible display
# if self.QC_ is None or self.n_spins <= 6:
# if self.n_spins < 9:
# self.QC_ = self.qc
self.QC_ = self.qc
self.QCI_ = i_state
self.QCH_ = circuit
self.QC2D_ = inverse_circuit
# Save circuits and subcircuits for possible display (explicitly set class variables)
if self.n_spins <= self.MAX_PRINT_SIZE:
HamiltonianKernel.QC_ = self.qc
HamiltonianKernel.QCI_ = i_state
HamiltonianKernel.QCH_ = hamiltonian_circuit
HamiltonianKernel.QC2D_ = inverse_circuit

# Collapse the sub-circuits used in this benchmark (for Qiskit)
qc2 = self.qc.decompose().decompose()
Expand All @@ -111,7 +113,7 @@ def initial_state(self) -> QuantumCircuit:
qc.h(0)
for k in range(1, self.n_spins):
qc.cx(k-1, k)
#self.QCI_ = qc

return qc

def create_hamiltonian(self) -> QuantumCircuit:
Expand Down Expand Up @@ -152,15 +154,11 @@ def kernel_draw(self):

# Print a sample circuit
print("Sample Circuit:")
if self.QC_ is not None and self.n_spins <= self.MAX_PRINT_SIZE:
if self.QC_ is not None:
print(self.QC_)
else:
print(" ... too large to print!")
return False

# we don't restrict save of large sub-circuits, so skip printing if num_qubits too large
#if self.QCI_ is not None and self.n_spins > 6:
#print("... subcircuits too large to print")
print(" ... circuit too large to print!")
return False

if self.QCI_ is not None:
print(" Initial State:")
Expand All @@ -177,6 +175,10 @@ def kernel_draw(self):
if self.QCRS_ is not None:
print(" Resultant Paulis:")
print(self.QCRS_)

# reset these variables after printing;
# (since we don't intialize them anywhere, they could be retained incorrectly on subsequent runs)
HamiltonianKernel.QC_ = HamiltonianKernel.QCI_ = HamiltonianKernel.QCH_ = HamiltonianKernel.QC2D_ = HamiltonianKernel.QCRS_ = None

return True

Expand Down

0 comments on commit 64a5163

Please sign in to comment.