Skip to content

Commit

Permalink
Fix parametric global phase in `QuantumCircuit.remove_final_measureme…
Browse files Browse the repository at this point in the history
…nts`

This was overlooked in the changes of 70901b2; the trick is that
`remove_final_measurements` does fairly deep in-place modifications to
the circuit, despite not logically being an in-place operation, so a lot
of internal data has to be touched and kept in sync.
  • Loading branch information
jakelishman committed Dec 21, 2023
1 parent 4b30598 commit ad14a64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,8 @@ def remove_final_measurements(self, inplace: bool = True) -> Optional["QuantumCi
# Clear instruction info
circ._data = CircuitData(qubits=circ._data.qubits, reserve=len(circ._data))
circ._parameter_table.clear()
# Repopulate the parameter table with any global-phase entries.
circ.global_phase = circ.global_phase

# We must add the clbits first to preserve the original circuit
# order. This way, add_register never adds clbits and just
Expand Down
18 changes: 18 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,24 @@ def test_remove_final_measurements_bit_locations(self):
self.assertEqual(circuit.find_bit(c0[0]), BitLocations(0, [(c0, 0)]))
self.assertEqual(circuit.find_bit(c3[0]), BitLocations(1, [(c3, 0)]))

def test_remove_final_measurements_parametric_global_phase(self):
"""Test that a parametric global phase is respected in the table afterwards."""
a = Parameter("a")
qc = QuantumCircuit(2, 2, global_phase=a)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])

expected = QuantumCircuit(2, global_phase=1)
expected.h(0)
expected.cx(0, 1)

self.assertEqual(
qc.remove_final_measurements(inplace=False).assign_parameters({a: 1}), expected
)
qc.remove_final_measurements(inplace=True)
self.assertEqual(qc.assign_parameters({a: 1}), expected)

def test_reverse(self):
"""Test reverse method reverses but does not invert."""
qc = QuantumCircuit(2, 2)
Expand Down

0 comments on commit ad14a64

Please sign in to comment.