Skip to content

Commit

Permalink
Merge pull request #35 from zapatacomputing/Morawiec/fix_warnings
Browse files Browse the repository at this point in the history
Fix: Fix all warnings in tests produced by Qiskit
  • Loading branch information
SebastianMorawiec authored Dec 18, 2023
2 parents cd05b99 + f0eb9ca commit 4f9f271
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ github_actions:
${VENV_NAME}/bin/python3 -m pip install --upgrade pip && \
${VENV_NAME}/bin/python3 -m pip install ./orquestra-quantum && \
${VENV_NAME}/bin/python3 -m pip install -e '.[dev]'

test:
$(PYTHON) -m pytest -W error tests

coverage:
$(PYTHON) -m pytest -W error\
--cov=src \
--cov-fail-under=$(MIN_COVERAGE) tests \
--no-cov-on-fail \
--cov-report xml \
&& echo Code coverage Passed the $(MIN_COVERAGE)% mark!
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def _import_qiskit_qubit(qubit: qiskit.circuit.Qubit) -> int:
return qubit.index
return qubit._index


def _qiskit_expr_from_orquestra(expr):
Expand Down
25 changes: 12 additions & 13 deletions src/orquestra/integrations/qiskit/conversions/_pauli_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
Translates OpenFermion Objects to qiskit SummedOp objects
"""
from orquestra.quantum.operators import PauliRepresentation, PauliSum, PauliTerm
from qiskit.opflow import PauliOp, SummedOp
from qiskit.quantum_info import Pauli
from qiskit.quantum_info import SparsePauliOp


def qubitop_to_qiskitpauli(operator: PauliRepresentation) -> SummedOp:
def qubitop_to_qiskitpauli(operator: PauliRepresentation) -> SparsePauliOp:
"""Convert a PauliRepresentation to a SummedOp.
Args:
Expand All @@ -42,12 +41,15 @@ def qubitop_to_qiskitpauli(operator: PauliRepresentation) -> SummedOp:
string_term = (
string_term[:term_qubit] + term_pauli + string_term[term_qubit + 1 :]
)
terms.append(PauliOp(Pauli(string_term), coeff=term.coefficient))
terms.append((string_term, term.coefficient))

return SummedOp(terms)
if not terms:
return SparsePauliOp("")
else:
return SparsePauliOp.from_list(terms)


def qiskitpauli_to_qubitop(qiskit_pauli: SummedOp) -> PauliSum:
def qiskitpauli_to_qubitop(qiskit_pauli: SparsePauliOp) -> PauliSum:
"""Convert a qiskit's SummedOp to a PauliSum.
Args:
Expand All @@ -56,17 +58,14 @@ def qiskitpauli_to_qubitop(qiskit_pauli: SummedOp) -> PauliSum:
Returns:
PauliSum representing the SummedOp
"""

if not isinstance(qiskit_pauli, SummedOp):
raise TypeError("qiskit_pauli must be a qiskit SummedOp")
if not isinstance(qiskit_pauli, SparsePauliOp):
raise TypeError("qiskit_pauli must be SparsePauliOp object")

transformed_operator = PauliSum()

for pauli_op in qiskit_pauli._oplist:
qiskit_term, weight = pauli_op.primitive, pauli_op.coeff

for qiskit_term, weight in zip(qiskit_pauli.paulis, qiskit_pauli.coeffs):
orquestra_term = PauliTerm.identity()
for (term_qubit, term_pauli) in enumerate(str(qiskit_term)):
for term_qubit, term_pauli in enumerate(str(qiskit_term)):
if term_pauli != "I":
if orquestra_term == PauliTerm.identity():
orquestra_term = PauliTerm(f"{term_pauli}{term_qubit}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
(_builtin_gates.S, qiskit.circuit.library.SGate()),
(_builtin_gates.SX, qiskit.circuit.library.SXGate()),
(_builtin_gates.T, qiskit.circuit.library.TGate()),
(_builtin_gates.CNOT, qiskit.extensions.CXGate()),
(_builtin_gates.CZ, qiskit.extensions.CZGate()),
(_builtin_gates.SWAP, qiskit.extensions.SwapGate()),
(_builtin_gates.ISWAP, qiskit.extensions.iSwapGate()),
(_builtin_gates.S.dagger, qiskit.extensions.SdgGate()),
(_builtin_gates.T.dagger, qiskit.extensions.TdgGate()),
(_builtin_gates.CNOT, qiskit.circuit.library.CXGate()),
(_builtin_gates.CZ, qiskit.circuit.library.CZGate()),
(_builtin_gates.SWAP, qiskit.circuit.library.SwapGate()),
(_builtin_gates.ISWAP, qiskit.circuit.library.iSwapGate()),
(_builtin_gates.S.dagger, qiskit.circuit.library.SdgGate()),
(_builtin_gates.T.dagger, qiskit.circuit.library.TdgGate()),
]

EQUIVALENT_PARAMETRIC_GATES = [
Expand All @@ -45,10 +45,10 @@
(_builtin_gates.RY, qiskit.circuit.library.RYGate),
(_builtin_gates.RZ, qiskit.circuit.library.RZGate),
(_builtin_gates.PHASE, qiskit.circuit.library.PhaseGate),
(_builtin_gates.CPHASE, qiskit.extensions.CPhaseGate),
(_builtin_gates.XX, qiskit.extensions.RXXGate),
(_builtin_gates.YY, qiskit.extensions.RYYGate),
(_builtin_gates.ZZ, qiskit.extensions.RZZGate),
(_builtin_gates.CPHASE, qiskit.circuit.library.CPhaseGate),
(_builtin_gates.XX, qiskit.circuit.library.RXXGate),
(_builtin_gates.YY, qiskit.circuit.library.RYYGate),
(_builtin_gates.ZZ, qiskit.circuit.library.RZZGate),
]
for theta in [0, -1, np.pi / 5, 2 * np.pi]
]
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_matrices_are_equal_up_to_phase_factor(self, theta, phi, lambda_):
orquestra_matrix = np.array(
_builtin_gates.U3(theta, phi, lambda_).matrix
).astype(np.complex128)
qiskit_matrix = qiskit.extensions.U3Gate(theta, phi, lambda_).to_matrix()
qiskit_matrix = qiskit.circuit.library.U3Gate(theta, phi, lambda_).to_matrix()

np.testing.assert_allclose(orquestra_matrix, qiskit_matrix, atol=1e-7)

Expand All @@ -127,7 +127,7 @@ def test_matrices_are_equal_up_to_phase_factor(self, theta, phi, lambda_):
_builtin_gates.U3(theta, phi, lambda_).controlled(1)(0, 1).lifted_matrix(2)
).astype(np.complex128)
qiskit_matrix = (
qiskit.extensions.U3Gate(theta, phi, lambda_).control(1).to_matrix()
qiskit.circuit.library.U3Gate(theta, phi, lambda_).control(1).to_matrix()
)

# Rearrange the qiskit matrix, such that it matches the endianness of orquestra
Expand Down Expand Up @@ -300,7 +300,10 @@ def _make_qiskit_circuit(n_qubits, commands, n_cbits=0):
[
(
"append",
(qiskit.extensions.U3Gate(np.pi / 5, np.pi / 2, np.pi / 4), [2]),
(
qiskit.circuit.library.U3Gate(np.pi / 5, np.pi / 2, np.pi / 4),
[2],
),
)
],
),
Expand All @@ -315,7 +318,7 @@ def _make_qiskit_circuit(n_qubits, commands, n_cbits=0):
(
"append",
(
qiskit.extensions.U3Gate(
qiskit.circuit.library.U3Gate(
np.pi / 5, np.pi / 2, np.pi / 4
).control(1),
[1, 2],
Expand Down Expand Up @@ -370,7 +373,7 @@ def _make_qiskit_circuit(n_qubits, commands, n_cbits=0):
(
"append",
(
qiskit.extensions.U3Gate(
qiskit.circuit.library.U3Gate(
QISKIT_THETA, QISKIT_GAMMA, QISKIT_LAMBDA
),
[3],
Expand All @@ -393,7 +396,7 @@ def _make_qiskit_circuit(n_qubits, commands, n_cbits=0):
(
"append",
(
qiskit.extensions.U3Gate(
qiskit.circuit.library.U3Gate(
QISKIT_THETA, QISKIT_GAMMA, QISKIT_LAMBDA
).control(1),
[2, 3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import pytest
from orquestra.quantum.operators import PauliSum, PauliTerm
from qiskit.opflow import PauliOp, SummedOp
from qiskit.quantum_info import Pauli
from qiskit.quantum_info import SparsePauliOp

from orquestra.integrations.qiskit.conversions import (
qiskitpauli_to_qubitop,
Expand All @@ -32,8 +31,7 @@ def test_translation_type_enforcement():
"""
sample_str = "Z0*Z1"
sample_int = 1
qiskit_op = SummedOp([PauliOp(Pauli("YXZIX"), 2.25)])

qiskit_op = SparsePauliOp(["YXZIX"], 2.25)
# don't accept anything other than orquestra PauliSum or PauliTerm
with pytest.raises(TypeError):
qubitop_to_qiskitpauli(sample_str)
Expand All @@ -51,9 +49,7 @@ def test_paulisum_to_qiskitpauli():

qiskit_op = qubitop_to_qiskitpauli(pauli_term)

ground_truth = (
PauliOp(Pauli("XZX"), 0.5) + PauliOp(Pauli("YZY"), 0.5)
).to_pauli_op()
ground_truth = SparsePauliOp(["XZX", "YZY"], coeffs=[0.5 + 0.0j, 0.5 + 0.0j])

assert ground_truth == qiskit_op

Expand All @@ -66,15 +62,15 @@ def test_pauliterm_to_qiskitpauli():

qiskit_op = qubitop_to_qiskitpauli(pauli_term)

ground_truth = SummedOp([PauliOp(Pauli("YXZIX"), 2.25)])
ground_truth = SparsePauliOp(["YXZIX"], [2.25 + 0.0j])

assert ground_truth == qiskit_op


def test_qubitop_to_qiskitpauli_zero():
zero_term = PauliSum()
qiskit_term = qubitop_to_qiskitpauli(zero_term)
ground_truth = SummedOp([])
ground_truth = SparsePauliOp("")

assert ground_truth == qiskit_term

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

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

assert test_pauli_term == expected_pauli_term


Expand Down
2 changes: 1 addition & 1 deletion tests/orquestra/integrations/qiskit/noise/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class TestBasic(unittest.TestCase):
def setUp(self):
self.ibmq_api_token = os.getenv("ZAPATA_IBMQ_API_TOKEN")
self.all_devices = ["ibm_nairobi"]
self.all_devices = ["ibm_kyoto"]
self.T_1 = 10e-7
self.T_2 = 30e-7
self.t_step = 10e-9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_qiskit_runner_can_run_job_with_sample_size_exceeding_backends_limit(
def noisy_simulator(request):
ibmq_api_token = os.getenv("ZAPATA_IBMQ_API_TOKEN")
noise_model, connectivity = get_qiskit_noise_model(
"ibm_nairobi", api_token=ibmq_api_token
"ibm_kyoto", api_token=ibmq_api_token
)
backend = Aer.get_backend(request.param)
return QiskitRunner(backend, noise_model=noise_model, coupling_map=connectivity)
Expand Down

0 comments on commit 4f9f271

Please sign in to comment.