qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

multi-control gate attributes not preserved by QPY

Open richrines1 opened this issue 1 year ago • 2 comments

Environment

  • Qiskit Terra version: 0.45.1
  • Python version: 3.11
  • Operating system: ubuntu

What is happening?

after QPY serialization + deserialization

  • the ctrl_state of multi-controlled gates (e.g. MCXGate, MCPhaseGate, ...) is reverted to its default value.
  • MCXVChain._dirty_ancillas is always False

How can we reproduce the issue?

circuit = qiskit.QuantumCircuit(6)
for gate in [
    qiskit.circuit.library.MCU1Gate(1.23, 5, ctrl_state=0),
    qiskit.circuit.library.MCXGate(5, ctrl_state=0),
    qiskit.circuit.library.MCXGrayCode(5, ctrl_state=0),
    qiskit.circuit.library.MCXRecursive(4, ctrl_state=0),
    qiskit.circuit.library.MCXVChain(3, ctrl_state=0, dirty_ancillas=False),
    qiskit.circuit.library.MCXVChain(3, ctrl_state=0, dirty_ancillas=True),
]:
    circuit.append(gate, range(gate.num_qubits))

print(circuit)
print(circuit[-1].operation._dirty_ancillas)

buf = io.BytesIO()
qiskit.qpy.dump(circuit, buf)
circuit = qiskit.qpy.load(io.BytesIO(buf.getvalue()))[0]

print(circuit)
print(circuit[-1].operation._dirty_ancillas)

prints:

q_0: ─o───────────o────o────o────o─────o───
      │           │    │    │    │     │   
q_1: ─o───────────o────o────o────o─────o───
      │           │    │    │    │     │   
q_2: ─o───────────o────o────o────o─────o───
      │           │    │    │  ┌─┴──┐┌─┴──┐
q_3: ─o───────────o────o────o──┤0   ├┤0   ├
      │           │    │  ┌─┴─┐│  X ││  X │
q_4: ─o───────────o────o──┤ X ├┤1   ├┤1   ├
      │U1(1.23) ┌─┴─┐┌─┴─┐└───┘└────┘└────┘
q_5: ─■─────────┤ X ├┤ X ├─────────────────
                └───┘└───┘                 
True
                                           
q_0: ─■───────────■────■────■────■─────■───
      │           │    │    │    │     │   
q_1: ─■───────────■────■────■────■─────■───
      │           │    │    │    │     │   
q_2: ─■───────────■────■────■────■─────■───
      │           │    │    │  ┌─┴──┐┌─┴──┐
q_3: ─■───────────■────■────■──┤0   ├┤0   ├
      │           │    │  ┌─┴─┐│  X ││  X │
q_4: ─■───────────■────■──┤ X ├┤1   ├┤1   ├
      │U1(1.23) ┌─┴─┐┌─┴─┐└───┘└────┘└────┘
q_5: ─■─────────┤ X ├┤ X ├─────────────────
                └───┘└───┘                 
False

What should happen?

both outputs should be the same, i.e. with open-controls, and _dirty_ancillas=True for the final operation

Any suggestions?

No response

richrines1 avatar Dec 06 '23 17:12 richrines1

Thanks for this report. I agree, this just looks like an oversight in QPY - these multi-controlled gate objects aren't heavily used in most cases (they're more properly compiler syntheses, but for historical reasons exposed through the circuit library), which is probably how they've got overlooked. You're right that they need their additional attributes handling specially in QPY.

jakelishman avatar Jan 03 '24 14:01 jakelishman

@jakelishman I'd be interested in tackling this issue as a first contribution to the Qiskit codebase. Could you assign this issue to me?

fbolton avatar Feb 14 '24 10:02 fbolton