Skip to content
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

Fix CircuitInstruction legacy iterable typing #12630

Merged
merged 1 commit into from
Jun 21, 2024
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
12 changes: 10 additions & 2 deletions crates/circuit/src/circuit_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ impl CircuitInstruction {

Ok(PyTuple::new_bound(
py,
[op, self.qubits.to_object(py), self.clbits.to_object(py)],
[
op,
self.qubits.bind(py).to_list().into(),
self.clbits.bind(py).to_list().into(),
],
))
}

Expand All @@ -558,7 +562,11 @@ impl CircuitInstruction {
};
Ok(PyTuple::new_bound(
py,
[op, self.qubits.to_object(py), self.clbits.to_object(py)],
[
op,
self.qubits.bind(py).to_list().into(),
self.clbits.bind(py).to_list().into(),
],
))
}

Expand Down
16 changes: 16 additions & 0 deletions test/python/circuit/test_circuit_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,22 @@ class TestQuantumCircuitInstructionData(QiskitTestCase):
# but are included as tests to maintain compatability with the previous
# list interface of circuit.data.

def test_iteration_of_data_entry(self):
"""Verify that the base types of the legacy tuple iteration are correct, since they're
different to attribute access."""
qc = QuantumCircuit(3, 3)
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.measure([0, 1, 2], [0, 1, 2])

def to_legacy(instruction):
return (instruction.operation, list(instruction.qubits), list(instruction.clbits))

expected = [to_legacy(instruction) for instruction in qc.data]
actual = [tuple(instruction) for instruction in qc.data]
self.assertEqual(actual, expected)

def test_getitem_by_insertion_order(self):
"""Verify one can get circuit.data items in insertion order."""
qr = QuantumRegister(2)
Expand Down
Loading