Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up device interface #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions graphix/device_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PatternRunner:
Executes the measurement pattern.
"""

def __init__(self, pattern: Pattern, backend: str = "ibmq", **kwargs) -> None:
def __init__(self, pattern: Pattern, backend: str = "ibmq", instance: str | None = None, resource: str | None = None, save_statevector: bool = False, optimizer_level: int = 1, shots: int = 1024) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update sadly does not contribute to type-stable interface (better than **kwargs though).
My ideas:

  • define ABC for backends
  • remove = "ibmq" : we do not need to add information about the name of the class inheriting ABC.

"""Instantiate a pattern runner.

Parameters
Expand All @@ -26,35 +26,39 @@ def __init__(self, pattern: Pattern, backend: str = "ibmq", **kwargs) -> None:
MBQC pattern to be executed.
backend: str
execution backend (optional, default is 'ibmq')
kwargs: dict
keyword args for specified backend.
instance: str | None
instance name (optional, backend specific)
resource: str | None
resource name (optional, backend specific)
instance: str | None
instance name (optional, backend specific)
save_statevector: bool
whether to save the statevector before the measurements of output qubits (optional, default is 'False', backend specific)
optimizer_level: int
optimizer level (optional, default is '1', backend specific)
shots: int
number of shots (optional, default is '1024', backend specific)
"""
self.pattern = pattern
self.backend_name = backend

if self.backend_name == "ibmq":
try:
from graphix_ibmq.runner import IBMQBackend
except Exception as e:
except ImportError as e:
raise ImportError(
"Failed to import graphix_ibmq. Please install graphix_ibmq by `pip install graphix-ibmq`."
) from e
self.backend = IBMQBackend(pattern)
try:
instance = kwargs.get("instance", "ibm-q/open/main")
resource = kwargs.get("resource", None)
save_statevector = kwargs.get("save_statevector", False)
optimization_level = kwargs.get("optimizer_level", 1)

self.backend.get_backend(instance, resource)
self.backend.to_qiskit(save_statevector)
self.backend.transpile(optimization_level)
self.shots = kwargs.get("shots", 1024)
except Exception:
save_statevector = kwargs.get("save_statevector", False)
optimization_level = kwargs.get("optimizer_level", 1)
self.backend.to_qiskit(save_statevector)
self.shots = kwargs.get("shots", 1024)
kwargs_get_backend = {}
if instance is not None:
kwargs_get_backend["instance"] = instance
if resource is not None:
kwargs_get_backend["resource"] = resource
self.backend.get_backend(**kwargs_get_backend)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: interface here is different in the latest commit of graphix-ibmq that is compatible with qiskit 1.0, to be merged soon.

self.backend.to_qiskit(save_statevector)
self.backend.transpile(optimizer_level)
self.shots = shots
else:
raise ValueError("unknown backend")

Expand Down
Loading