-
Notifications
You must be signed in to change notification settings - Fork 24
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
get num of qubits from api or use 36 and fix EM sampler #188
Changes from 6 commits
b8d7741
717b080
3194d19
0d54968
e5e33a9
c1ba492
d61fe36
fe87158
3854245
20a72bb
547322b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,9 @@ | |
import base64 | ||
import platform | ||
import warnings | ||
import os | ||
import requests | ||
from dotenv import dotenv_values | ||
|
||
from qiskit import __version__ as qiskit_terra_version | ||
from qiskit.circuit import controlledgate as q_cgates | ||
|
@@ -423,7 +426,7 @@ def qiskit_to_ionq( | |
settings = passed_args.get("job_settings") or None | ||
if settings is not None: | ||
ionq_json["settings"] = settings | ||
error_mitigation = passed_args.get("error_mitigation") | ||
error_mitigation = passed_args.get("error_mitigation") or backend.options.error_mitigation | ||
if error_mitigation and isinstance(error_mitigation, ErrorMitigation): | ||
ionq_json["error_mitigation"] = error_mitigation.value | ||
return json.dumps(ionq_json, cls=SafeEncoder) | ||
|
@@ -474,6 +477,73 @@ def default(self, o): | |
return "unknown" | ||
|
||
|
||
|
||
def resolve_credentials(token: str = None, url: str = None): | ||
"""Resolve credentials for use in IonQ API calls. | ||
|
||
If the provided ``token`` and ``url`` are both ``None``, then these values | ||
are loaded from the ``IONQ_API_TOKEN`` and ``IONQ_API_URL`` | ||
environment variables, respectively. | ||
|
||
If no url is discovered, then ``https://api.ionq.co/v0.3`` is used. | ||
|
||
Args: | ||
token (str): IonQ API access token. | ||
url (str, optional): IonQ API url. Defaults to ``None``. | ||
|
||
Returns: | ||
dict[str]: A dict with "token" and "url" keys, for use by a client. | ||
""" | ||
env_token = ( | ||
dotenv_values().get("QISKIT_IONQ_API_TOKEN") # first check for dotenv values | ||
or dotenv_values().get("IONQ_API_KEY") | ||
or dotenv_values().get("IONQ_API_TOKEN") | ||
or os.getenv("QISKIT_IONQ_API_TOKEN") # then check for global env values | ||
or os.getenv("IONQ_API_KEY") | ||
or os.getenv("IONQ_API_TOKEN") | ||
) | ||
env_url = ( | ||
dotenv_values().get("QISKIT_IONQ_API_URL") | ||
or dotenv_values().get("IONQ_API_URL") | ||
or os.getenv("QISKIT_IONQ_API_URL") | ||
or os.getenv("IONQ_API_URL") | ||
) | ||
return { | ||
"token": token or env_token, | ||
"url": url or env_url or "https://api.ionq.co/v0.3", | ||
} | ||
|
||
|
||
|
||
def get_n_qubits(backend: str) -> int: | ||
"""Get the number of qubits for a given backend. | ||
|
||
Args: | ||
backend (str): The name of the backend. | ||
|
||
Returns: | ||
int: The number of qubits for the backend. | ||
""" | ||
url, token = resolve_credentials().values() | ||
try: | ||
return requests.get( | ||
url=f"{url}/characterizations/backends/{backend}/current", | ||
headers={"Authorization": f"apiKey {token}"}, | ||
timeout=5, | ||
).json()["qubits"] | ||
except Exception: # pylint: disable=broad-except | ||
if backend == "ionq_qpu.harmony": | ||
return 11 | ||
elif backend == "ionq_qpu.aria-1": | ||
return 25 | ||
elif backend == "ionq_qpu.aria-2": | ||
return 25 | ||
elif backend == "ionq_qpu.forte-1": | ||
return 36 | ||
else: | ||
return 36 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have a hard time understanding what this code was doing, since it falls back during an exception but doesn't' log it or what value it's deciding. Could you add logs here? (or alternatively, fall back to Infinity or something >> 36?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can't do inifinity because the transpiler freezes (i guess it tries to create a graph of length #qubits lol |
||
|
||
|
||
__all__ = [ | ||
"qiskit_to_ionq", | ||
"qiskit_circ_to_ionq_circ", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this? falling back to a hard-coded value when the API is down feels a little weird
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the user might not have an api_key set, and we require that to get characterization data (including the number of qubits)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah I meant more, the part where we code in the current system sizes, would worry it could quickly become stale, like the new method with warning and a fixed cap, thanks for changing!