Skip to content

Commit

Permalink
Added config parameter transpile_optimization_option.
Browse files Browse the repository at this point in the history
Added Fake_Guadalupe as selectable Backend
  • Loading branch information
Maximilian Wolf authored and dieser-max committed Dec 7, 2023
1 parent 36e0515 commit 5860a7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ def preprocess(self, input_data: dict, config: Config, **kwargs):

output = self.sequence_to_circuit(input_data)
backend = self.select_backend(config["backend"])
output["execute_circuit"] = self.get_execute_circuit(
try:
output["execute_circuit"] = 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["circuit"],
backend,
config["backend"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from modules.training.QCBM import QCBM
from modules.training.Inference import Inference
from modules.applications.QML.generative_modeling.mappings.Library import Library

from time import perf_counter

Check failure on line 27 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#L27

Standard import "from time import perf_counter" should be placed before "from qiskit import QuantumCircuit" (wrong-import-order, C0411)
logging.getLogger("qiskit").setLevel(logging.WARNING)


Expand Down Expand Up @@ -89,7 +89,7 @@ def get_parameter_options(self) -> dict:
"backend": {
"values": ["aer_statevector_simulator_gpu", "aer_statevector_simulator_cpu",
"cusvaer_simulator (only available in cuQuantum applicance)", "aer_simulator_gpu",
"aer_simulator_cpu", "ionQ_Harmony", "Amazon_SV1"],
"aer_simulator_cpu","aer_sim_gpu_Fake_Guadalupe","aer_sim_cpu_Fake_Guadalupe" ,"ionQ_Harmony", "Amazon_SV1"],

Check failure on line 92 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#L92

Line too long (136/120) (line-too-long, C0301)
"description": "Which backend do you want to use? (aer_statevector_simulator\
uses the measurement probability vector, the others are shot based)"
},
Expand All @@ -98,6 +98,11 @@ def get_parameter_options(self) -> dict:
"values": [100, 1000, 10000, 1000000],
"description": "How many shots do you want use for estimating the PMF of the model?\
(If the aer_statevector_simulator selected, only relevant for studying generalization)"
},

"transpile_optimization_level": {
"values": [1, 2, 3, 0],
"description": "Switch between different optimizaition levels in the Qiskit Transpile routine. 1: light optimization, 2: heavy optimization, 3: even heavier optimization, 0: no optimization. 1 as standard option"

Check failure on line 105 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#L105

Line too long (228/120) (line-too-long, C0301)
}
}

Expand Down Expand Up @@ -201,6 +206,17 @@ def select_backend(config: str) -> dict:
from qiskit import Aer # pylint: disable=C0415
backend = Aer.get_backend("aer_simulator")
backend.set_options(device="GPU")

Check failure on line 209 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#L209

Trailing whitespace (trailing-whitespace, C0303)
elif config in ["aer_sim_cpu_Fake_Guadalupe","aer_sim_gpu_Fake_Guadalupe"]:
from qiskit.providers.fake_provider import FakeGuadalupeV2

Check failure on line 211 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#L211

Import outside toplevel (qiskit.providers.fake_provider.FakeGuadalupeV2) (import-outside-toplevel, C0415)
from qiskit.providers.aer import AerSimulator

Check failure on line 212 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#L212

Import outside toplevel (qiskit.providers.aer.AerSimulator) (import-outside-toplevel, C0415)
backend = AerSimulator.from_backend(FakeGuadalupeV2())
if config=="aer_sim_gpu_Fake_Guadalupe":
device_to_use = "GPU"
else:
device_to_use = "CPU"
backend.set_options(device=device_to_use)


elif config == "aer_simulator_cpu":
from qiskit import Aer # pylint: disable=C0415
Expand Down Expand Up @@ -249,7 +265,7 @@ def select_backend(config: str) -> dict:
return backend

@staticmethod
def get_execute_circuit(circuit: QuantumCircuit, backend: Backend, config: str, n_shots: int) -> callable:
def get_execute_circuit(circuit: QuantumCircuit, backend: Backend, config: str, n_shots: int, transpile_optimization_level = 1) -> callable:

Check failure on line 268 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#L268

Line too long (144/120) (line-too-long, C0301)
"""
This method combnines the qiskit circuit implemenation and the selected backend and returns a function,
that will be called during training.
Expand All @@ -265,8 +281,13 @@ def get_execute_circuit(circuit: QuantumCircuit, backend: Backend, config: str,
:return: Method that executes the quantum circuit for a given set of parameters
:rtype: callable
"""
start = perf_counter()
n_qubits = circuit.num_qubits
circuit_transpiled = transpile(circuit, backend=backend)
circuit_transpiled = transpile(circuit, backend=backend, optimization_level = transpile_optimization_level)
logging.info(f"Using Transpile optimization level {transpile_optimization_level}")
logging.info(f"With the following gates: {circuit_transpiled.count_ops()}")
logging.info(f"Process took {perf_counter() - start} seconds")
logging.info(f"{backend=}")

if config in ["aer_statevector_simulator_gpu", "aer_statevector_simulator_cpu"]:
circuit_transpiled.remove_final_measurements()
Expand Down Expand Up @@ -304,7 +325,7 @@ def execute_circuit(solutions):
return pmfs, samples

elif config in ["cusvaer_simulator (only available in cuQuantum applicance)", "aer_simulator_cpu",
"aer_simulator_gpu"]:
"aer_simulator_gpu","aer_sim_gpu_Fake_Guadalupe","aer_sim_cpu_Fake_Guadalupe"]:
def execute_circuit(solutions):
all_circuits = [circuit_transpiled.bind_parameters(solution) for solution in solutions]
qobjs = assemble(all_circuits, backend=backend)
Expand Down

0 comments on commit 5860a7d

Please sign in to comment.