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

Authenticate IBM Quantum account with GH actions to run unit tests #268

Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions .github/workflows/functional_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
env:
IBM_API_TOKEN: ${{ secrets.IBM_API_TOKEN }}
run: |
python -m pytest -m "not skip"
- name: Install TorchQuantum
Expand Down
7 changes: 2 additions & 5 deletions test/plugin/test_qiskit_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
SOFTWARE.
"""

from qiskit import QuantumCircuit
import numpy as np
import random
from qiskit.opflow import StateFn, X, Y, Z, I
Expand All @@ -32,17 +31,15 @@
from torchquantum.plugin import op_history2qiskit, QiskitProcessor
from torchquantum.util import switch_little_big_endian_state

import torch
import pytest

pauli_str_op_dict = {
"X": X,
"Y": Y,
"Z": Z,
"I": I,
}

@pytest.mark.skip

# @pytest.mark.skip
def test_expval_observable():
# seed = 0
# random.seed(seed)
Expand Down
10 changes: 6 additions & 4 deletions torchquantum/plugin/qiskit/qiskit_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
SOFTWARE.
"""

import os
import torch
import torchquantum as tq
import pathos.multiprocessing as multiprocessing
import itertools

from qiskit import Aer, execute, IBMQ, transpile, QuantumCircuit
from qiskit import Aer, execute, transpile, QuantumCircuit
from qiskit.providers.aer.noise import NoiseModel
from qiskit.tools.monitor import job_monitor
from qiskit.exceptions import QiskitError
Expand All @@ -38,7 +39,6 @@
)
from torchquantum.util import (
get_expectations_from_counts,
get_provider,
get_provider_hub_group_project,
get_circ_stats,
)
Expand Down Expand Up @@ -72,7 +72,7 @@ def run_job_worker(data):
break
except Exception as e:
if "Job was cancelled" in str(e):
logger.warning(f"Job is cancelled manually.")
logger.warning("Job is cancelled manually.")
return None
else:
logger.warning(f"Job failed because {e}, rerun now.")
Expand Down Expand Up @@ -191,8 +191,10 @@ def qiskit_init(self):

if self.backend is None:
# initialize now
IBMQ.load_account()
# Retrieve token from environment.
token = os.getenv("IBM_API_TOKEN")
self.provider = get_provider_hub_group_project(
token=token,
hub=self.hub,
group=self.group,
project=self.project,
Expand Down
16 changes: 8 additions & 8 deletions torchquantum/util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def switch_little_big_endian_state(state):
is_batch_state = False
reshape = [2] * int(np.log2(state.size))
else:
logger.exception(f"Dimension of statevector should be 1 or 2")
logger.exception("Dimension of statevector should be 1 or 2")
raise ValueError

original_shape = state.shape
Expand Down Expand Up @@ -467,7 +467,7 @@ def build_module_from_op_list(
]
module = build_module_from_op_list(op_list, remove_ops=True, thres=0.1)
"""
logger.info(f"Building module from op_list...")
logger.info("Building module from op_list...")
thres = 1e-5 if thres is None else thres
n_removed_ops = 0
ops = []
Expand Down Expand Up @@ -499,7 +499,7 @@ def build_module_from_op_list(
if n_removed_ops > 0:
logger.warning(f"Remove in total {n_removed_ops} pruned operations.")
else:
logger.info(f"Do not remove any operations.")
logger.info("Do not remove any operations.")

return tq.QuantumModuleFromOps(ops)

Expand Down Expand Up @@ -770,8 +770,8 @@ def get_provider(backend_name, hub=None):
try:
provider = QiskitRuntimeService(channel = "ibm_quantum", instance = "ibm-q-research/mass-inst-tech-1/main")
except QiskitError:
# logger.warning(f"Cannot use MIT backend, roll back to open")
logger.warning(f"Use the open backend")
# logger.warning("Cannot use MIT backend, roll back to open")
logger.warning("Use the open backend")
provider = QiskitRuntimeService(channel = "ibm_quantum", instance = "ibm-q/open/main")
elif hub == "mit":
provider = QiskitRuntimeService(channel = "ibm_quantum", instance = "ibm-q-research/MIT-1/main")
Expand All @@ -781,8 +781,8 @@ def get_provider(backend_name, hub=None):
return provider


def get_provider_hub_group_project(hub="ibm-q", group="open", project="main"):
provider = QiskitRuntimeService(channel = "ibm_quantum", instance = f"{hub}/{group}/{project}")
def get_provider_hub_group_project(token, hub="ibm-q", group="open", project="main"):
provider = QiskitRuntimeService(channel = "ibm_quantum", token=token, instance = f"{hub}/{group}/{project}")
return provider


Expand Down Expand Up @@ -1048,7 +1048,7 @@ def clone_model(model_to_clone):#i have to note:this clone_model function was ma
#####################

for idx, key in enumerate(state_dict_plus_shift):
if idx < 2: # Skip the first two keys because they are not paramters
if idx < 2: # Skip the first two keys because they are not parameters
continue
state_dict_plus_shift[key] += shift_rate
state_dict_minus_shift[key] -= shift_rate
Expand Down
Loading