Spaces:
Sleeping
Sleeping
harishaseebat92
commited on
Commit
·
50d95a5
1
Parent(s):
e5dfd1f
Secret Token Issue Resolved
Browse files
qlbm/qlbm_sample_app.py
CHANGED
|
@@ -574,7 +574,12 @@ def run_sampling_hw_ibm(
|
|
| 574 |
log("Connecting to IBM Quantum service...")
|
| 575 |
update_progress(20, "Connecting to IBM Quantum...")
|
| 576 |
|
| 577 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
backend = service.least_busy()
|
| 579 |
log(f"Selected backend: {backend.name}")
|
| 580 |
update_progress(25, f"Backend: {backend.name}")
|
|
@@ -697,7 +702,21 @@ def run_sampling_hw_ibm(
|
|
| 697 |
|
| 698 |
from qiskit_ionq import IonQProvider
|
| 699 |
|
| 700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
|
| 702 |
def run_sampling_hw_ionq(
|
| 703 |
n,
|
|
@@ -760,6 +779,13 @@ def run_sampling_hw_ionq(
|
|
| 760 |
log("Step 1: Generating quantum circuits...")
|
| 761 |
update_progress(5, "Generating quantum circuits...")
|
| 762 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
# backend = provider.get_backend("simulator")
|
| 764 |
backend = provider.get_backend("qpu.forte-enterprise-1")
|
| 765 |
# Use backend.name (property) instead of backend.name() (method) for Qiskit compatibility
|
|
|
|
| 574 |
log("Connecting to IBM Quantum service...")
|
| 575 |
update_progress(20, "Connecting to IBM Quantum...")
|
| 576 |
|
| 577 |
+
ibm_token = _require_env("API_KEY_IBM_QLBM", context="IBM QLBM QPU execution")
|
| 578 |
+
service = QiskitRuntimeService(
|
| 579 |
+
channel="ibm_cloud",
|
| 580 |
+
token=ibm_token,
|
| 581 |
+
instance="crn:v1:bluemix:public:quantum-computing:us-east:a/15157e4350c04a9dab51b8b8a4a93c86:e29afd91-64bf-4a82-8dbf-731e6c213595::",
|
| 582 |
+
)
|
| 583 |
backend = service.least_busy()
|
| 584 |
log(f"Selected backend: {backend.name}")
|
| 585 |
update_progress(25, f"Backend: {backend.name}")
|
|
|
|
| 702 |
|
| 703 |
from qiskit_ionq import IonQProvider
|
| 704 |
|
| 705 |
+
|
| 706 |
+
class MissingCredentialError(RuntimeError):
|
| 707 |
+
"""Raised when a required API key/secret is not available at runtime."""
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
def _require_env(var_name: str, *, context: str) -> str:
|
| 711 |
+
"""Return a required environment variable or raise a helpful error."""
|
| 712 |
+
value = os.environ.get(var_name)
|
| 713 |
+
if value is None or str(value).strip() == "":
|
| 714 |
+
raise MissingCredentialError(
|
| 715 |
+
f"Missing required secret '{var_name}'. "
|
| 716 |
+
f"Set it as a runtime environment variable (e.g., Hugging Face Space → Settings → Secrets) "
|
| 717 |
+
f"before running {context}."
|
| 718 |
+
)
|
| 719 |
+
return value
|
| 720 |
|
| 721 |
def run_sampling_hw_ionq(
|
| 722 |
n,
|
|
|
|
| 779 |
log("Step 1: Generating quantum circuits...")
|
| 780 |
update_progress(5, "Generating quantum circuits...")
|
| 781 |
|
| 782 |
+
# Ensure credentials are present (HF secrets) and make them discoverable
|
| 783 |
+
# by qiskit-ionq's default environment variable names.
|
| 784 |
+
ionq_token = _require_env("API_KEY_IONQ_QLBM", context="IonQ QLBM QPU execution")
|
| 785 |
+
os.environ.setdefault("IONQ_API_TOKEN", ionq_token)
|
| 786 |
+
|
| 787 |
+
provider = IonQProvider()
|
| 788 |
+
|
| 789 |
# backend = provider.get_backend("simulator")
|
| 790 |
backend = provider.get_backend("qpu.forte-enterprise-1")
|
| 791 |
# Use backend.name (property) instead of backend.name() (method) for Qiskit compatibility
|
utils/EBU_Quantum/no_body/base_functions.py
CHANGED
|
@@ -6,6 +6,21 @@ import time
|
|
| 6 |
import psutil
|
| 7 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from scipy.special import jn
|
| 11 |
from scipy.sparse import identity, csr_matrix, kron, diags, eye
|
|
@@ -338,10 +353,13 @@ def get_absolute_field_values(all_circuits, shots, pm_optimization_level, simula
|
|
| 338 |
elif simulation == "False" and platform == "IBM":
|
| 339 |
_log("Connecting to IBM Quantum service...")
|
| 340 |
_progress(42, "Connecting to IBM Quantum...")
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
| 345 |
backend = service.least_busy(operational=True, simulator=False)
|
| 346 |
_log(f"Selected IBM QPU backend: {backend.name}")
|
| 347 |
_progress(45, f"Backend: {backend.name}")
|
|
@@ -374,7 +392,10 @@ def get_absolute_field_values(all_circuits, shots, pm_optimization_level, simula
|
|
| 374 |
_log("Connecting to IonQ service...")
|
| 375 |
_progress(42, "Connecting to IonQ...")
|
| 376 |
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
| 378 |
ionq_backend = provider.get_backend("qpu.forte-enterprise-1")
|
| 379 |
_log(f"Selected IonQ backend: {ionq_backend.name()}")
|
| 380 |
_progress(45, f"Backend: {ionq_backend.name()}")
|
|
|
|
| 6 |
import psutil
|
| 7 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 8 |
import os
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class MissingCredentialError(RuntimeError):
|
| 12 |
+
"""Raised when a required API key/secret is not available at runtime."""
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _require_env(var_name: str, *, context: str) -> str:
|
| 16 |
+
value = os.environ.get(var_name)
|
| 17 |
+
if value is None or str(value).strip() == "":
|
| 18 |
+
raise MissingCredentialError(
|
| 19 |
+
f"Missing required secret '{var_name}'. "
|
| 20 |
+
f"Set it as a runtime environment variable (e.g., Hugging Face Space → Settings → Secrets) "
|
| 21 |
+
f"before running {context}."
|
| 22 |
+
)
|
| 23 |
+
return value
|
| 24 |
import matplotlib.pyplot as plt
|
| 25 |
from scipy.special import jn
|
| 26 |
from scipy.sparse import identity, csr_matrix, kron, diags, eye
|
|
|
|
| 353 |
elif simulation == "False" and platform == "IBM":
|
| 354 |
_log("Connecting to IBM Quantum service...")
|
| 355 |
_progress(42, "Connecting to IBM Quantum...")
|
| 356 |
+
|
| 357 |
+
ibm_token = _require_env("API_KEY_IBM_EM", context="IBM EM QPU execution")
|
| 358 |
+
service = QiskitRuntimeService(
|
| 359 |
+
channel="ibm_cloud",
|
| 360 |
+
token=ibm_token,
|
| 361 |
+
instance="crn:v1:bluemix:public:quantum-computing:us-east:a/15157e4350c04a9dab51b8b8a4a93c86:e29afd91-64bf-4a82-8dbf-731e6c213595::",
|
| 362 |
+
)
|
| 363 |
backend = service.least_busy(operational=True, simulator=False)
|
| 364 |
_log(f"Selected IBM QPU backend: {backend.name}")
|
| 365 |
_progress(45, f"Backend: {backend.name}")
|
|
|
|
| 392 |
_log("Connecting to IonQ service...")
|
| 393 |
_progress(42, "Connecting to IonQ...")
|
| 394 |
|
| 395 |
+
ionq_token = _require_env("API_KEY_IONQ_EM", context="IonQ EM QPU execution")
|
| 396 |
+
os.environ.setdefault("IONQ_API_TOKEN", ionq_token)
|
| 397 |
+
|
| 398 |
+
provider = IonQProvider()
|
| 399 |
ionq_backend = provider.get_backend("qpu.forte-enterprise-1")
|
| 400 |
_log(f"Selected IonQ backend: {ionq_backend.name()}")
|
| 401 |
_progress(45, f"Backend: {ionq_backend.name()}")
|