Skip to content

Commit c100656

Browse files
mergify[bot]ElePT
andauthored
Fix behavior of HLS plugins after preserve_order=False (backport Qiskit#14539) (Qiskit#14547)
* Fix HLS preserve order (Qiskit#14539) (cherry picked from commit 2982004) # Conflicts: # qiskit/transpiler/passes/synthesis/hls_plugins.py * Update hls_plugins.py --------- Co-authored-by: Elena Peña Tapia <[email protected]>
1 parent f6bae2c commit c100656

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

qiskit/transpiler/passes/synthesis/hls_plugins.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,13 +1585,15 @@ def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **
15851585
# Don't do anything if a gate is called "evolution" but is not an
15861586
# actual PauliEvolutionGate
15871587
return None
1588-
15891588
algo = high_level_object.synthesis
15901589

1590+
original_preserve_order = algo.preserve_order
15911591
if "preserve_order" in options and isinstance(algo, ProductFormula):
15921592
algo.preserve_order = options["preserve_order"]
15931593

1594-
return algo.synthesize(high_level_object)
1594+
synth_object = algo.synthesize(high_level_object)
1595+
algo.preserve_order = original_preserve_order
1596+
return synth_object
15951597

15961598

15971599
class PauliEvolutionSynthesisRustiq(HighLevelSynthesisPlugin):
@@ -1642,6 +1644,7 @@ def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **
16421644
)
16431645
return None
16441646

1647+
original_preserve_order = algo.preserve_order
16451648
if "preserve_order" in options:
16461649
algo.preserve_order = options["preserve_order"]
16471650

@@ -1654,7 +1657,7 @@ def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **
16541657
upto_phase = options.get("upto_phase", False)
16551658
resynth_clifford_method = options.get("resynth_clifford_method", 1)
16561659

1657-
return synth_pauli_network_rustiq(
1660+
synth_object = synth_pauli_network_rustiq(
16581661
num_qubits=num_qubits,
16591662
pauli_network=pauli_network,
16601663
optimize_count=optimize_count,
@@ -1663,3 +1666,5 @@ def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **
16631666
upto_phase=upto_phase,
16641667
resynth_clifford_method=resynth_clifford_method,
16651668
)
1669+
algo.preserve_order = original_preserve_order
1670+
return synth_object
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a bug in the :class:`.PauliEvolutionSynthesisDefault` and :class:`.PauliEvolutionSynthesisRustiq` plugins that
5+
modified the `.synthesis` attribute of the original circuit when setting ``preserve_order=False``. The behavior of the
6+
plugins has been restored and the original circuit is now preserved throughout the transpilation pipeline.

test/python/transpiler/test_high_level_synthesis.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,22 @@ def test_trivial_rotations(self, plugin_name):
26902690
self.assertEqual(Operator(qc), Operator(qct))
26912691
self.assertEqual(count_rotation_gates(qct), 1)
26922692

2693+
def test_default_preserve_order(self):
2694+
"""Test that option preserve_order is reset."""
2695+
op = SparsePauliOp(["IIIX", "IIXX", "IYYI", "IIZZ"], coeffs=[1, 2, 3, 4])
2696+
qc = QuantumCircuit(6)
2697+
qc.append(PauliEvolutionGate(op), [1, 2, 3, 4])
2698+
with self.subTest("preserve_order_is_reset"):
2699+
hls_config = HLSConfig(PauliEvolution=[("default", {"preserve_order": False})])
2700+
hls_pass = HighLevelSynthesis(hls_config=hls_config)
2701+
qct = hls_pass(qc)
2702+
self.assertEqual(qct.depth(), 3)
2703+
# check that preserve_order is reset and is no longer False
2704+
hls_config = HLSConfig(PauliEvolution=[("default", {})])
2705+
hls_pass = HighLevelSynthesis(hls_config=hls_config)
2706+
qct = hls_pass(qc)
2707+
self.assertEqual(qct.depth(), 4)
2708+
26932709
def test_rustiq_upto_options(self):
26942710
"""Test non-default Rustiq options upto_phase and upto_clifford."""
26952711
op = SparsePauliOp(["XXXX", "YYYY", "ZZZZ"], coeffs=[1, 2, 3])
@@ -2742,6 +2758,20 @@ def test_rustiq_preserve_order(self):
27422758
cnt_ops = qct.count_ops()
27432759
self.assertEqual(count_rotation_gates(qct), 6)
27442760
self.assertEqual(cnt_ops["cx"], 4)
2761+
with self.subTest("preserve_order_is_reset"):
2762+
hls_config = HLSConfig(PauliEvolution=[("rustiq", {"preserve_order": False})])
2763+
hls_pass = HighLevelSynthesis(hls_config=hls_config)
2764+
qct = hls_pass(qc)
2765+
cnt_ops = qct.count_ops()
2766+
self.assertEqual(count_rotation_gates(qct), 6)
2767+
self.assertEqual(cnt_ops["cx"], 4)
2768+
# check that preserve_order is reset and is no longer False
2769+
hls_config = HLSConfig(PauliEvolution=[("rustiq", {})])
2770+
hls_pass = HighLevelSynthesis(hls_config=hls_config)
2771+
qct = hls_pass(qc)
2772+
cnt_ops = qct.count_ops()
2773+
self.assertEqual(count_rotation_gates(qct), 6)
2774+
self.assertEqual(cnt_ops["cx"], 16)
27452775

27462776
def test_rustiq_upto_phase(self):
27472777
"""Check that Rustiq synthesis with ``upto_phase=True`` produces a correct

0 commit comments

Comments
 (0)