Skip to content

Commit

Permalink
making metadata optional for resource estimates
Browse files Browse the repository at this point in the history
  • Loading branch information
JonhasSC committed May 15, 2024
1 parent 7ecc488 commit 999cdcf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 17 additions & 7 deletions src/qca/utils/algo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import numpy as np
import networkx as nx
from dataclasses import asdict

from cirq import Circuit
from cirq.contrib import qasm_import
Expand All @@ -19,16 +20,16 @@
from pyLIQTR.gate_decomp.cirq_transforms import clifford_plus_t_direct_transform
from pyLIQTR.phase_factors.fourier_response.fourier_response import Angler_fourier_response

from qca.utils.utils import circuit_estimate, estimate_cpt_resources, EstimateMetaData
from qca.utils.utils import circuit_estimate, estimate_cpt_resources, EstimateMetaData, re_as_json

def estimate_qsp(
pyliqtr_hamiltonian: Hamiltonian,
evolution_time:float,
numsteps:int,
energy_precision:float,
metadata: EstimateMetaData,
outdir:str,
hamiltonian_name:str='hamiltonian',
metadata: EstimateMetaData = None,
write_circuits:bool=False,
) -> Circuit:
timestep_of_interest=evolution_time/numsteps
Expand All @@ -45,14 +46,17 @@ def estimate_qsp(
t1 = time.perf_counter()
elapsed = t1 - t0
print(f'Time to generate high level QSP circuit: {elapsed} seconds')
circuit_estimate(
outfile = f'{outdir}{hamiltonian_name}_re.json'
logical_re = circuit_estimate(
circuit=qsp_circuit,
metadata=metadata,
outdir=outdir,
numsteps=numsteps,
circuit_name=hamiltonian_name,
write_circuits=write_circuits
)
if metadata:
re_metadata = asdict(metadata)
logical_re = re_metadata | logical_re
re_as_json(logical_re, outfile)
return qsp_circuit

def find_hamiltonian_ordering(of_hamiltonian: QubitOperator) -> list:
Expand Down Expand Up @@ -95,8 +99,8 @@ def estimate_trotter(
openfermion_hamiltonian: QubitOperator,
evolution_time: float,
energy_precision: float,
metadata: EstimateMetaData,
outdir:str,
metadata: EstimateMetaData=None,
hamiltonian_name:str='hamiltonian',
write_circuits:bool=False,
nsteps:int=None
Expand Down Expand Up @@ -146,12 +150,18 @@ def estimate_trotter(
with open(outfile_qasm_cpt, 'w', encoding='utf-8') as f:
print_to_openqasm(f, cpt_trotter, qubits=cpt_trotter.all_qubits())

estimate_cpt_resources(
logical_re = estimate_cpt_resources(
cpt_circuit=cpt_trotter,
metadata=metadata,
outdir=outdir,
is_extrapolated=True,
circuit_name=hamiltonian_name,
trotter_steps=nsteps
)

outfile = f'{outdir}{hamiltonian_name}_re.json'
if metadata:
re_metadata = asdict(metadata)
logical_re = re_metadata | logical_re
re_as_json(logical_re, outfile)
return cpt_trotter
18 changes: 5 additions & 13 deletions src/qca/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ def gen_resource_estimate(

def estimate_cpt_resources(
cpt_circuit: cirq.AbstractCircuit,
metadata: EstimateMetaData,
outdir: str,
is_extrapolated:bool,
circuit_name:str,
Expand All @@ -162,23 +161,17 @@ def estimate_cpt_resources(
os.makedirs(outdir)

total_steps = trotter_steps * magnus_steps
re_metadata = asdict(metadata)
re_metadata['Logical_Abstract'] = gen_resource_estimate(
logical_re = gen_resource_estimate(
cpt_circuit=cpt_circuit,
is_extrapolated=is_extrapolated,
total_steps=total_steps
)
resource_estimate = re_metadata

outfile_data = f'{outdir}{circuit_name}_re.json'
re_as_json(resource_estimate, outfile_data)
return logical_re

def circuit_estimate(
circuit: cirq.AbstractCircuit,
metadata: EstimateMetaData,
outdir: str,
numsteps: int,
circuit_name: str,
bits_precision:int=1,
write_circuits:bool = False
) -> cirq.AbstractCircuit:
Expand Down Expand Up @@ -241,17 +234,16 @@ def circuit_estimate(
total_T_depth += subcircuit_counts[gate][0] * t_depth * numsteps
total_T_count += subcircuit_counts[gate][0] * t_count * numsteps
total_clifford_count += subcircuit_counts[gate][0] * clifford_count * numsteps
re_metadata = asdict(metadata)
re_metadata['Logical_Abstract'] = {

return {'Logical_Abstract': {
'num_qubits': len(circuit.all_qubits()),
't_count': total_T_count,
'circuit_depth': total_gate_depth,
'gate_count': total_gate_count,
't_depth': total_T_depth,
'clifford_count': total_clifford_count
}
}
outfile_data = f'{outdir}{circuit_name}_re.json'
re_as_json(re_metadata, outfile_data)

def re_as_json(main_estimate:dict, outdir:str) -> None:
with open(outdir, 'w') as f:
Expand Down

0 comments on commit 999cdcf

Please sign in to comment.