Skip to content

Commit

Permalink
Add default args to experimental average
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathonMisiewicz committed May 20, 2024
1 parent ad591ab commit 76b5633
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/qforte/abc/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def measure_energy(self, Ucirc):
val = np.real(myQC.direct_op_exp_val(self._qb_ham))
else:
Exp = qforte.Experiment(self._nqb, Ucirc, self._qb_ham, 2000)
val = Exp.perfect_experimental_avg([])
val = Exp.perfect_experimental_avg()

assert np.isclose(np.imag(val), 0.0)

Expand Down
2 changes: 1 addition & 1 deletion src/qforte/abc/qsdabc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QSD(Algorithm):
such as the Hartree-Fock state.
Quantum subspace diagonalization methods work by constructing an effective
Hamiltonain :math:`\mathbf{H}` with matrix elements given by
Hamiltonian :math:`\mathbf{H}` with matrix elements given by
.. math::
H_{mn} = \\langle \Psi_m | \hat{H} | \Psi_n \\rangle,
Expand Down
6 changes: 3 additions & 3 deletions src/qforte/experiment/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(
self, n_qubits, generator, operator, N_samples, prepare_each_time=False
):
"""
Experimant is a class that exemplifies two quantum computational tasks:
Experiment is a class that exemplifies two quantum computational tasks:
(1) state preparation from a 'generator' circuit which may or may not be
parameterized, and (2) to measure operators to produce approximate
expectation values.
Expand All @@ -38,7 +38,7 @@ def __init__(
self.N_samples_ = N_samples
self.prepare_each_time_ = prepare_each_time

def experimental_avg(self, params):
def experimental_avg(self, params = []):
"""
calculates the experimental average of the operator the Experiment object was initialized with
Expand Down Expand Up @@ -75,7 +75,7 @@ def experimental_avg(self, params):
"No support yet for measurement with multiple state preparations"
)

def perfect_experimental_avg(self, params):
def perfect_experimental_avg(self, params = []):
"""
calculates the exact experimental result of the operator the Experiment object was initialized with
Expand Down
30 changes: 9 additions & 21 deletions src/qforte/qkd/srqk.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,13 @@ def build_qk_mats_fast(self):

if self._diagonalize_each_step:
print("\n\n")

print(
f"{'k(S)':>7}{'E(Npar)':>19}{'N(params)':>14}{'N(CNOT)':>18}{'N(measure)':>20}"
)
print(
"-------------------------------------------------------------------------------"
)
string = f"{'k(S)':>7}{'E(Npar)':>19}{'N(params)':>14}{'N(CNOT)':>18}{'N(measure)':>20}\n"
string += "-------------------------------------------------------------------------------"
print(string)

if self._print_summary_file:
f = open("summary.dat", "w+", buffering=1)
f.write(
f"#{'k(S)':>7}{'E(Npar)':>19}{'N(params)':>14}{'N(CNOT)':>18}{'N(measure)':>20}\n"
)
f.write(
"#-------------------------------------------------------------------------------\n"
)
f.write(string + "\n")

for m in range(self._nstates):
# Compute U_m = exp(-i m dt H)
Expand Down Expand Up @@ -226,7 +217,7 @@ def build_qk_mats_realistic(self):

return s_mat, h_mat

# TODO depricate this function
# TODO deprecate this function
def matrix_element(self, m, n, use_op=False):
"""Returns a single matrix element M_mn based on the evolution of
two unitary operators Um = exp(-i * m * dt * H) and Un = exp(-i * n * dt * H)
Expand Down Expand Up @@ -314,9 +305,8 @@ def matrix_element(self, m, n, use_op=False):
X_exp = qforte.Experiment(self._nqb + 1, cir, X_op, 100)
Y_exp = qforte.Experiment(self._nqb + 1, cir, Y_op, 100)

params = [1.0]
x_value = X_exp.perfect_experimental_avg(params)
y_value = Y_exp.perfect_experimental_avg(params)
x_value = X_exp.perfect_experimental_avg()
y_value = Y_exp.perfect_experimental_avg()

value = (x_value + 1.0j * y_value) * phase1 * np.conj(phase2)

Expand Down Expand Up @@ -362,10 +352,8 @@ def matrix_element(self, m, n, use_op=False):
X_exp = qforte.Experiment(self._nqb + 1, cir, X_op, 100)
Y_exp = qforte.Experiment(self._nqb + 1, cir, Y_op, 100)

# TODO (cleanup): Remove params as required arg (Nick)
params = [1.0]
x_value = X_exp.perfect_experimental_avg(params)
y_value = Y_exp.perfect_experimental_avg(params)
x_value = X_exp.perfect_experimental_avg()
y_value = Y_exp.perfect_experimental_avg()

element = (x_value + 1.0j * y_value) * phase1 * np.conj(phase2)
value += c * element
Expand Down
6 changes: 2 additions & 4 deletions tests/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def test_H2_experiment(self):
circ.add(gate("X", 1, 1))

TestExperiment = Experiment(4, circ, H2_qubit_hamiltonian, 1000000)
params2 = []
avg_energy = TestExperiment.experimental_avg(params2)
avg_energy = TestExperiment.experimental_avg()
print("Measured H2 Experimental Avg. Energy")
print(avg_energy)
print("H2 RHF Energy")
Expand Down Expand Up @@ -118,8 +117,7 @@ def test_H2_experiment_perfect(self):
circ.add(gate("X", 1, 1))

TestExperiment = Experiment(4, circ, H2_qubit_hamiltonian, 1000000)
params2 = []
avg_energy = TestExperiment.perfect_experimental_avg(params2)
avg_energy = TestExperiment.perfect_experimental_avg()
print("Perfectly Measured H2 Experimental Avg. Energy")
print(avg_energy)
print("H2 RHF Energy")
Expand Down

0 comments on commit 76b5633

Please sign in to comment.