-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add transpile_operator function to primitives.utils #9988
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,6 @@ | ||||||
--- | ||||||
features: | ||||||
- | | ||||||
Add :func:`~primitives.utils.transpile_operator` function. | ||||||
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'm not sure this will be found as it is, I think it would be safer to either add the full path, or use
Suggested change
|
||||||
If skip_transpilation is True, users need to transpile the operator corresponding to the layout | ||||||
of the transpiled circuit. The function helps the transpilation of operator. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,17 @@ | |
|
||
"""Tests for utilities of Primitives.""" | ||
|
||
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister | ||
from qiskit.primitives.utils import final_measurement_mapping | ||
from test import combine | ||
|
||
from ddt import ddt | ||
|
||
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile | ||
from qiskit.primitives import BackendEstimator | ||
from qiskit.primitives.utils import final_measurement_mapping, transpile_operator | ||
from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2 | ||
from qiskit.quantum_info import SparsePauliOp | ||
from qiskit.test import QiskitTestCase | ||
from qiskit.utils import optionals | ||
|
||
|
||
class TestMapping(QiskitTestCase): | ||
|
@@ -83,3 +91,31 @@ def test_mapping_w_delays(self): | |
|
||
maps = final_measurement_mapping(qc) | ||
self.assertDictEqual(maps, {1: 0, 0: 1}) | ||
|
||
|
||
BACKENDS = [FakeNairobi(), FakeNairobiV2()] | ||
|
||
|
||
@ddt | ||
class TestTranspileOperator(QiskitTestCase): | ||
"""Test transpile_operator utility function.""" | ||
|
||
@combine(backend=BACKENDS) | ||
def test_tranpile_operator(self, backend): | ||
"""test for transpile_operator""" | ||
backend.set_options(seed_simulator=15) | ||
n = 6 | ||
qc = QuantumCircuit(n) | ||
qc.x(n - 1) | ||
qc.h(range(n)) | ||
qc.cx(range(n - 1), n - 1) | ||
qc.h(range(n - 1)) | ||
trans_qc = transpile(qc, backend, seed_transpiler=15) | ||
|
||
op = SparsePauliOp("Z" * n) | ||
trans_op = transpile_operator(op, trans_qc.layout, qc.qubits) | ||
result = BackendEstimator(backend=backend).run(trans_qc, trans_op).result() | ||
if optionals.HAS_AER: | ||
self.assertAlmostEqual(result.values[0], -0.045, places=2) | ||
else: | ||
self.assertAlmostEqual(result.values[0], 0, places=2) | ||
Comment on lines
+117
to
+121
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. Could you add an additional test that checks that 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. sure. |
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.
Should we use
collections.abc
instead oftyping
here?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.
Oh... What’s wrong with me