Skip to content

Commit 07e61c8

Browse files
authored
Consistent sparse list format in sparse operators (#14067)
* Consistent sparse list format in sparse operators * review comments - use combine in test - fix comment
1 parent 0bf91a6 commit 07e61c8

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

crates/accelerate/src/sparse_observable/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,14 +2931,11 @@ impl PySparseObservable {
29312931
let to_py_tuple = |view: SparseTermView| {
29322932
let mut pauli_string = String::with_capacity(view.bit_terms.len());
29332933

2934-
// we reverse the order of bits and indices so the Pauli string comes out in
2935-
// "reading order", consistent with how one would write the label in
2936-
// SparseObservable.from_list or .from_label
2937-
for bit in view.bit_terms.iter().rev() {
2934+
for bit in view.bit_terms.iter() {
29382935
pauli_string.push_str(bit.py_label());
29392936
}
29402937
let py_string = PyString::new(py, &pauli_string).unbind();
2941-
let py_indices = PyList::new(py, view.indices.iter().rev())?.unbind();
2938+
let py_indices = PyList::new(py, view.indices.iter())?.unbind();
29422939
let py_coeff = view.coeff.into_py_any(py)?;
29432940

29442941
PyTuple::new(py, vec![py_string.as_any(), py_indices.as_any(), &py_coeff])

test/python/circuit/library/test_evolution_gate.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from qiskit.converters import circuit_to_dag
2828
from qiskit.quantum_info import Operator, SparsePauliOp, Pauli, Statevector, SparseObservable
2929
from qiskit.transpiler.passes import HLSConfig, HighLevelSynthesis
30-
from test import QiskitTestCase # pylint: disable=wrong-import-order
30+
from test import QiskitTestCase, combine # pylint: disable=wrong-import-order
3131

3232
X = SparsePauliOp("X")
3333
Y = SparsePauliOp("Y")
@@ -316,11 +316,10 @@ def test_dag_conversion(self):
316316

317317
self.assertEqual(ops, expected_ops)
318318

319-
@data("chain", "fountain")
320-
def test_cnot_chain_options(self, option):
319+
@combine(option=["chain", "fountain"], use_sparse_observable=[True, False])
320+
def test_cnot_chain_options(self, option, use_sparse_observable):
321321
"""Test selecting different kinds of CNOT chains."""
322-
323-
op = Z ^ Z ^ Z
322+
op = SparseObservable("ZZZ") if use_sparse_observable else SparsePauliOp(["ZZZ"])
324323
synthesis = LieTrotter(reps=1, cx_structure=option)
325324
evo = PauliEvolutionGate(op, synthesis=synthesis)
326325

@@ -595,7 +594,7 @@ def test_projector_circuit(self):
595594
-1 +1 +1 -1 -1 +1 // eigenvalue
596595
1 0 0 1 1 0 // ctrl state
597596
-----------------
598-
--> P(-1).ctrl_state(00110) applied on qubits [0, 1, 2, 3, 4, 5]
597+
--> [X P(-1) X].ctrl_state(11001) applied on qubits [5, 4, 3, 2, 1, 0]
599598
600599
"""
601600
op = SparseObservable("-+rl10")
@@ -605,7 +604,9 @@ def test_projector_circuit(self):
605604
reference.sx([2, 3])
606605
reference.h([4, 5])
607606

608-
reference.append(PhaseGate(-1.0).control(5, ctrl_state="00110"), reference.qubits)
607+
reference.x(0)
608+
reference.append(PhaseGate(-1.0).control(5, ctrl_state="11001"), reference.qubits[::-1])
609+
reference.x(0)
609610

610611
reference.sxdg([2, 3])
611612
reference.h([4, 5])

0 commit comments

Comments
 (0)