Skip to content

Commit

Permalink
Fix pickling of conditioned custom gates from OpenQASM 2 import (Qisk…
Browse files Browse the repository at this point in the history
…it#11175) (Qiskit#11258)

The condition of a gate defined by a `gate` statement in an OpenQASM 2
program was not previously exported as part of its state during
pickling, causing pickle roundtrips or deepcopies to lose the condition
on the new copy.

(cherry picked from commit 196221f)

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
mergify[bot] and jakelishman authored Nov 16, 2023
1 parent ff85be7 commit a40811e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 3 additions & 2 deletions qiskit/qasm2/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,15 @@ def _define(self):
# to pickle ourselves, we just eagerly create the definition and pickle that.

def __getstate__(self):
return (self.name, self.num_qubits, self.params, self.definition)
return (self.name, self.num_qubits, self.params, self.definition, self.condition)

def __setstate__(self, state):
name, num_qubits, params, definition = state
name, num_qubits, params, definition, condition = state
super().__init__(name, num_qubits, params)
self._gates = ()
self._bytecode = ()
self._definition = definition
self._condition = condition


def _gate_builder(name, num_qubits, known_gates, bytecode):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Conditioned custom gates imported from OpenQASM 2 will now correctly retain their conditions
when pickled and deep-copied. Previously, any conditional custom gate (defined by a ``gate``
statement in an OpenQASM 2 file) would lose its condition when copied or pickled.
27 changes: 27 additions & 0 deletions test/python/qasm2/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring

import copy
import io
import math
import os
Expand Down Expand Up @@ -696,6 +697,32 @@ def test_qpy_double_call_roundtrip(self):
loaded = qpy.load(fptr)[0]
self.assertEqual(loaded, qc)

def test_deepcopy_conditioned_defined_gate(self):
program = """
include "qelib1.inc";
gate my_gate a {
x a;
}
qreg q[1];
creg c[1];
if (c == 1) my_gate q[0];
"""
parsed = qiskit.qasm2.loads(program)
my_gate = parsed.data[0].operation

self.assertEqual(my_gate.name, "my_gate")
self.assertEqual(my_gate.condition, (parsed.cregs[0], 1))

copied = copy.deepcopy(parsed)
copied_gate = copied.data[0].operation
self.assertEqual(copied_gate.name, "my_gate")
self.assertEqual(copied_gate.condition, (copied.cregs[0], 1))

pickled = pickle.loads(pickle.dumps(parsed))
pickled_gate = pickled.data[0].operation
self.assertEqual(pickled_gate.name, "my_gate")
self.assertEqual(pickled_gate.condition, (pickled.cregs[0], 1))


class TestOpaque(QiskitTestCase):
def test_simple(self):
Expand Down

0 comments on commit a40811e

Please sign in to comment.