Skip to content

Commit

Permalink
Added a option to store the transpiled circuit in the benchmark direc…
Browse files Browse the repository at this point in the history
…troy. Readded transpile option for now.
  • Loading branch information
Maximilian Wolf committed Jan 12, 2024
1 parent 0dc6c24 commit cae429e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np
import pandas as pd
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
from qiskit import qpy

from modules.Core import *
from utils import start_time_measurement, end_time_measurement
Expand Down Expand Up @@ -102,6 +103,10 @@ def postprocess(self, input_data: dict, config: dict, **kwargs):
with open(f"{store_dir_iter}/samples_complete_{kwargs['rep_count']}.pkl", 'wb') as f:
pickle.dump(input_data.pop("samples_complete"), f)

if "circuit_transpiled" in list(input_data.keys()):
with open(f"{store_dir_iter}/transpiled_circuit_{kwargs['rep_count']}.qpy", 'wb') as f:
qpy.dump(input_data.pop("circuit_transpiled"), f)

evaluation, _ = self.evaluate(input_data)

if self.generalization_mark is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ def preprocess(self, input_data: dict, config: Config, **kwargs):
output = self.sequence_to_circuit(input_data)
backend = self.select_backend(config["backend"])
try:
output["execute_circuit"] = self.get_execute_circuit(
output["execute_circuit"], output['circuit_transpiled'] = self.get_execute_circuit(

Check failure on line 70 in src/modules/applications/QML/generative_modeling/mappings/Library.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/Library.py#L70

Too many positional arguments for staticmethod call (too-many-function-args, E1121)
output["circuit"],
backend,
config["backend"],
config["n_shots"],
config["transpile_optimization_level"])
except:

Check failure on line 76 in src/modules/applications/QML/generative_modeling/mappings/Library.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/Library.py#L76

No exception type(s) specified (bare-except, W0702)
output["execute_circuit"] = self.get_execute_circuit(
output["execute_circuit"], output['circuit_transpiled'] = self.get_execute_circuit(
output["circuit"],
backend,
config["backend"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ def get_execute_circuit(circuit: QuantumCircuit, backend: Backend, config: str,
"""
n_qubits = circuit.num_qubits

#readded this option, since this variable will be stored
circuit_transpiled = transpile(circuit, backend=backend, optimization_level=transpile_optimization_level, seed_transpiler=42)

Check failure on line 287 in src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py#L287

Line too long (133/120) (line-too-long, C0301)

if transpile_optimization_level == -1:
from mqt.predictor.rl import Predictor, qcompile

Check failure on line 290 in src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py#L290

Import outside toplevel (mqt.predictor.rl.Predictor, mqt.predictor.rl.qcompile) (import-outside-toplevel, C0415)
pred = Predictor(figure_of_merit="expected_fidelity", device_name="ibm_guadalupe")
Expand Down Expand Up @@ -331,10 +334,14 @@ def execute_circuit(solutions):
if transpile_optimization_level in range(4):
all_circuit_transpiled = [transpile(circuit, backend=backend, optimization_level=transpile_optimization_level) for circuit in all_circuits]

Check failure on line 335 in src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py#L335

Line too long (159/120) (line-too-long, C0301)



Check failure on line 338 in src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py#L338

Trailing whitespace (trailing-whitespace, C0303)
elif transpile_optimization_level==-1:
all_circuit_transpiled = [qcompile(circuit, figure_of_merit="expected_fidelity", device_name="ibm_guadalupe", predictor_singleton=pred) [0] for circuit in all_circuits]

Check failure on line 340 in src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/mappings/LibraryQiskit.py#L340

Line too long (188/120) (line-too-long, C0301)

qobjs = assemble(all_circuit_transpiled, backend=backend)
#circuit_transpiled = qobjs; if possible

jobs = backend.run(qobjs, shots=n_shots)
samples_dictionary = [jobs.result().get_counts(circuit).int_outcomes() for circuit in all_circuits]

Expand All @@ -351,4 +358,4 @@ def execute_circuit(solutions):
pmfs = samples / n_shots
return pmfs, samples

return execute_circuit
return execute_circuit, circuit_transpiled
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def reverse_transform(self, input_data: dict) -> (any, float):
n_registers = self.transform_config["n_registers"]
KL_best_transformed = min(input_data["KL"])
best_results = input_data["best_sample"]
circuit_transpiled = input_data['circuit_transpiled']

Check failure on line 136 in src/modules/applications/QML/generative_modeling/transformations/PIT.py

View workflow job for this annotation

GitHub Actions / Pylint

src/modules/applications/QML/generative_modeling/transformations/PIT.py#L136

Trailing whitespace (trailing-whitespace, C0303)

array_bins = self.compute_discretization_efficient(n_qubits, n_registers)
transformed_samples = self.generate_samples_efficient(best_results, array_bins, n_registers, noisy=True)
Expand Down Expand Up @@ -169,7 +171,8 @@ def reverse_transform(self, input_data: dict) -> (any, float):
"histogram_generated_original": histogram_generated_original,
"histogram_generated": histogram_generated_transformed,
"KL_best_transformed": KL_best_transformed,
"store_dir_iter": input_data["store_dir_iter"]
"store_dir_iter": input_data["store_dir_iter"],
"circuit_transpiled": circuit_transpiled
}

return reverse_config_trans
Expand Down

0 comments on commit cae429e

Please sign in to comment.