Skip to content

Commit

Permalink
Merge pull request #23 from zapatacomputing/zqs-1202-upgrade-qiskit
Browse files Browse the repository at this point in the history
feat: upgraded qiskit to .38
  • Loading branch information
Athena Caesura authored Oct 5, 2022
2 parents c6b8323 + e6fb4a1 commit 5e8587f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ install_requires =
numpy>=1.20
scipy>=1.4.1
sympy>=1.5,<=1.9
qiskit>=0.28, <=0.34
qiskit==0.38
symengine~=0.7
orquestra-quantum

Expand Down
15 changes: 6 additions & 9 deletions src/orquestra/integrations/qiskit/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
from orquestra.quantum.circuits import Circuit
from orquestra.quantum.measurements import Measurements
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, execute
from qiskit.ignis.mitigation.measurement import (
CompleteMeasFitter,
MeasurementFilter,
complete_meas_cal,
)
from qiskit.providers.ibmq import IBMQ
from qiskit.providers.ibmq.exceptions import (
IBMQAccountError,
Expand All @@ -23,6 +18,8 @@
)
from qiskit.providers.ibmq.job import IBMQJob
from qiskit.result import Counts
from qiskit.utils.mitigation import CompleteMeasFitter, complete_meas_cal
from qiskit.utils.mitigation._filters import MeasurementFilter

from orquestra.integrations.qiskit.conversions import export_to_qiskit

Expand Down Expand Up @@ -137,9 +134,10 @@ def run_circuitset_and_measure(
n_samples_for_experiments,
multiplicities,
) = self.transform_circuitset_to_ibmq_experiments(circuits, n_samples)
batches, n_samples_for_batches = self.batch_experiments(
experiments, n_samples_for_experiments
)
(
batches,
n_samples_for_batches,
) = self.batch_experiments(experiments, n_samples_for_experiments)

jobs = [
self.execute_with_retries(batch, n_samples)
Expand Down Expand Up @@ -317,7 +315,6 @@ def aggregate_measurements(
circuit_set_from_jobs = [] # circuits that qiskit ran
circuit_set_from_batches = [] # circuits that users sent
self.list_virtual_to_physical_qubits_dict = []

circuit_counts_set = []
for job, batch in zip(jobs, batches):
circuit_set_from_jobs.extend(job.circuits())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def qubitop_to_qiskitpauli(operator: PauliRepresentation) -> SummedOp:
string_term = (
string_term[:term_qubit] + term_pauli + string_term[term_qubit + 1 :]
)
terms.append(PauliOp(Pauli.from_label(string_term), coeff=term.coefficient))
terms.append(PauliOp(Pauli(string_term), coeff=term.coefficient))

return SummedOp(terms)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Every time the Qiskit version is updated, the pickled files need to be regenerated.
This script regenerates the pickled files."""

import os
import pickle

from orquestra.quantum.circuits import CNOT, Circuit, X

from orquestra.integrations.qiskit.backend import QiskitBackend

token = "YOUR_TOKEN_HERE"
file_path = os.path.dirname(os.path.realpath(__file__))


def pickle_jobs_and_batches_with_different_qubits():
"""Create pickle file for jobs and batches with different qubits."""

# make orquestra circuit
circuits = [Circuit([X(0), CNOT(0, 3), CNOT(1, 2)])]
n_samples = 50
lima_backend = QiskitBackend("ibmq_lima", api_token=token)

(
experiments,
n_samples_for_experiments,
multiplicities,
) = lima_backend.transform_circuitset_to_ibmq_experiments(circuits, [n_samples])
(
batches,
n_samples_for_batches,
) = lima_backend.batch_experiments(experiments, n_samples_for_experiments)

jobs = [
lima_backend.execute_with_retries(batch, n_samples)
for n_samples, batch in zip(n_samples_for_batches, batches)
]
for job in jobs:
job.result()

with open(
os.path.join(file_path, "jobs_and_batches_with_different_qubits.pickle"), "wb"
) as f:
pickle.dump(jobs, f)
pickle.dump(batches, f)
pickle.dump(multiplicities, f)


if __name__ == "__main__":
pickle_jobs_and_batches_with_different_qubits()
18 changes: 10 additions & 8 deletions tests/orquestra/integrations/qiskit/backend/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,31 @@ def test_aggregate_measurements(self, backend):
)
assert len(measurements_set) == 2

def test_aggregate_measurements_extract_correct_qubits(
def test_aggregate_measurements_extracts_correct_qubits(
self, backend_with_readout_correction
):
# Given
extracted_pickle_objects = open(
with open(
os.path.join(
os.path.dirname(__file__),
"jobs_and_batches_with_different_qubits.pickle",
),
"rb",
)
jobs = pickle.load(extracted_pickle_objects)
batches = pickle.load(extracted_pickle_objects)
extracted_pickle_objects.close()
multiplicities = [1]
) as f:
jobs = pickle.load(f)
batches = pickle.load(f)
multiplicities = pickle.load(f)

# When
results = backend_with_readout_correction.aggregate_measurements(
jobs, batches, multiplicities
)

# Then
assert results[0].get_counts() == {"11": 50}
if backend_with_readout_correction.noise_inversion_method == "pseudo_inverse":
assert results[0].get_counts() == {"0000": 1, "1001": 48, "1101": 1}
else:
assert results[0].get_counts() == {"1001": 48}

def test_run_circuitset_and_measure(self, backend):
# Given
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_translation_type_enforcement():
"""
sample_str = "Z0*Z1"
sample_int = 1
qiskit_op = SummedOp([PauliOp(Pauli.from_label("YXZIX"), 2.25)])
qiskit_op = SummedOp([PauliOp(Pauli("YXZIX"), 2.25)])

# don't accept anything other than orquestra PauliSum or PauliTerm
with pytest.raises(TypeError):
Expand All @@ -52,7 +52,7 @@ def test_paulisum_to_qiskitpauli():
qiskit_op = qubitop_to_qiskitpauli(pauli_term)

ground_truth = (
PauliOp(Pauli.from_label("XZX"), 0.5) + PauliOp(Pauli.from_label("YZY"), 0.5)
PauliOp(Pauli("XZX"), 0.5) + PauliOp(Pauli("YZY"), 0.5)
).to_pauli_op()

assert ground_truth == qiskit_op
Expand All @@ -66,7 +66,7 @@ def test_pauliterm_to_qiskitpauli():

qiskit_op = qubitop_to_qiskitpauli(pauli_term)

ground_truth = SummedOp([PauliOp(Pauli.from_label("YXZIX"), 2.25)])
ground_truth = SummedOp([PauliOp(Pauli("YXZIX"), 2.25)])

assert ground_truth == qiskit_op

Expand All @@ -83,7 +83,7 @@ def test_qiskitpauli_to_qubitop():
"""
Conversion of qiskit SummedOp to PauliSum; accuracy test
"""
qiskit_term = SummedOp([PauliOp(Pauli.from_label("XIIIIY"), coeff=1)])
qiskit_term = SummedOp([PauliOp(Pauli("XIIIIY"), coeff=1)])

expected_pauli_term = PauliTerm.from_iterable([("X", 0), ("Y", 5)])
test_pauli_term = qiskitpauli_to_qubitop(qiskit_term)
Expand Down

0 comments on commit 5e8587f

Please sign in to comment.