Skip to content

Consistent sparse list format in sparse operators (backport #14067) #14110

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions crates/accelerate/src/sparse_observable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2931,14 +2931,11 @@ impl PySparseObservable {
let to_py_tuple = |view: SparseTermView| {
let mut pauli_string = String::with_capacity(view.bit_terms.len());

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

PyTuple::new(py, vec![py_string.as_any(), py_indices.as_any(), &py_coeff])
Expand Down
15 changes: 8 additions & 7 deletions test/python/circuit/library/test_evolution_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from qiskit.converters import circuit_to_dag
from qiskit.quantum_info import Operator, SparsePauliOp, Pauli, Statevector, SparseObservable
from qiskit.transpiler.passes import HLSConfig, HighLevelSynthesis
from test import QiskitTestCase # pylint: disable=wrong-import-order
from test import QiskitTestCase, combine # pylint: disable=wrong-import-order

X = SparsePauliOp("X")
Y = SparsePauliOp("Y")
Expand Down Expand Up @@ -316,11 +316,10 @@ def test_dag_conversion(self):

self.assertEqual(ops, expected_ops)

@data("chain", "fountain")
def test_cnot_chain_options(self, option):
@combine(option=["chain", "fountain"], use_sparse_observable=[True, False])
def test_cnot_chain_options(self, option, use_sparse_observable):
"""Test selecting different kinds of CNOT chains."""

op = Z ^ Z ^ Z
op = SparseObservable("ZZZ") if use_sparse_observable else SparsePauliOp(["ZZZ"])
synthesis = LieTrotter(reps=1, cx_structure=option)
evo = PauliEvolutionGate(op, synthesis=synthesis)

Expand Down Expand Up @@ -595,7 +594,7 @@ def test_projector_circuit(self):
-1 +1 +1 -1 -1 +1 // eigenvalue
1 0 0 1 1 0 // ctrl state
-----------------
--> P(-1).ctrl_state(00110) applied on qubits [0, 1, 2, 3, 4, 5]
--> [X P(-1) X].ctrl_state(11001) applied on qubits [5, 4, 3, 2, 1, 0]

"""
op = SparseObservable("-+rl10")
Expand All @@ -605,7 +604,9 @@ def test_projector_circuit(self):
reference.sx([2, 3])
reference.h([4, 5])

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

reference.sxdg([2, 3])
reference.h([4, 5])
Expand Down