From 999cdcfa2d5c2613d1e2b4bd795f3cece7a7a5bc Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 15 May 2024 16:51:53 -0600 Subject: [PATCH] making metadata optional for resource estimates --- src/qca/utils/algo_utils.py | 24 +++++++++++++++++------- src/qca/utils/utils.py | 18 +++++------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/qca/utils/algo_utils.py b/src/qca/utils/algo_utils.py index cfc5f9a..eb46392 100644 --- a/src/qca/utils/algo_utils.py +++ b/src/qca/utils/algo_utils.py @@ -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 @@ -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 @@ -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: @@ -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 @@ -146,7 +150,7 @@ 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, @@ -154,4 +158,10 @@ def estimate_trotter( 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 diff --git a/src/qca/utils/utils.py b/src/qca/utils/utils.py index cd739f2..0c059df 100644 --- a/src/qca/utils/utils.py +++ b/src/qca/utils/utils.py @@ -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, @@ -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: @@ -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: