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 qpy for MCX gates #9391

Merged
merged 5 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _read_instruction(file_obj, circuit, registers, custom_operations, version,
if gate_name in {"IfElseOp", "WhileLoopOp"}:
gate = gate_class(condition_tuple, *params)
elif version >= 5 and issubclass(gate_class, ControlledGate):
if gate_name in {"MCPhaseGate", "MCU1Gate"}:
if gate_name in {"MCPhaseGate", "MCU1Gate", "MCXGrayCode"}:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just curious if there are other gates in the mcx family that have the same constructor and need this. I always get lost in all the mcx variants. But it might be good to check we've got all of them covered here

Copy link
Contributor Author

@ElePT ElePT Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right @mtreinish, qpy also fails for other MCX gates. Namely (I think these are all):

MCXRecursive
MCXVChain
MCXGate

I can add these to the PR. I though I had tested it before realizing that I was using 2 control qubits, so the gate ended up being of type CCXGate (of course my checks passed).

gate = gate_class(*params, instruction.num_ctrl_qubits)
else:
gate = gate_class(*params)
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/fix-qpy-mcxgray-421cf8f673f24238.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug in QPY (:mod:`qiskit.qpy`) where circuits containing gates of class
:class:`.MCXGrayCode` would fail to serialize.
See `#9390 <https://github.com/Qiskit/qiskit-terra/issues/9390>`__.
14 changes: 12 additions & 2 deletions test/python/circuit/test_circuit_load_from_qpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
from qiskit.circuit.quantumregister import Qubit
from qiskit.circuit.random import random_circuit
from qiskit.circuit.gate import Gate
from qiskit.circuit.library import XGate, QFT, QAOAAnsatz, PauliEvolutionGate, DCXGate, MCU1Gate
from qiskit.circuit.library import (
XGate,
QFT,
QAOAAnsatz,
PauliEvolutionGate,
DCXGate,
MCU1Gate,
MCXGrayCode,
)
from qiskit.circuit.instruction import Instruction
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.parametervector import ParameterVector
Expand Down Expand Up @@ -1043,9 +1051,11 @@ def test_open_controlled_gate(self):

def test_standard_control_gates(self):
"""Test standard library controlled gates."""
qc = QuantumCircuit(3)
qc = QuantumCircuit(6)
mtreinish marked this conversation as resolved.
Show resolved Hide resolved
mcu1_gate = MCU1Gate(np.pi, 2)
mcx_gray_gate = MCXGrayCode(5)
qc.append(mcu1_gate, [0, 2, 1])
qc.append(mcx_gray_gate, list(range(0, 6)))
qc.mcp(np.pi, [0, 2], 1)
qc.mct([0, 2], 1)
qc.mcx([0, 2], 1)
Expand Down