question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QPY serialization fails for circuits with IfElse instructions

See original GitHub issue

Environment

  • Qiskit Terra version: 0.20.0.dev0+01913f4
  • Python version: 3.7
  • Operating system: Mac OSX 11.6.1

What is happening?

QPY serialization fails when attempting to serialize a circuit containing an IfElseOp.

How can we reproduce the issue?

import qiskit as qk

qc = qk.QuantumCircuit(2, 2)
qc.h(0)
qc.measure(0, 0)

with qc.if_test((qc.clbits[0], True)):
    qc.x(1)
    
qc.measure(1,1)

from qiskit.circuit import qpy_serialization as qpy
import tempfile

with tempfile.TemporaryFile() as fp:
    qpy.dump(qc, fp)
    out = qpy.load(fp)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/97/vkqk0hs17r5dnlc8dv5nmfxh0000gn/T/ipykernel_34451/736355381.py in <module>
      3 
      4 with tempfile.TemporaryFile() as fp:
----> 5     qpy.dump(qc, fp)
      6     out = qpy.load(fp)

~/q/qiskit-terra/qiskit/circuit/qpy_serialization.py in dump(circuits, file_obj)
   1423     file_obj.write(header)
   1424     for circuit in circuits:
-> 1425         _write_circuit(file_obj, circuit)
   1426 
   1427 

~/q/qiskit-terra/qiskit/circuit/qpy_serialization.py in _write_circuit(file_obj, circuit)
   1493     index_map["c"] = clbit_indices
   1494     for instruction in circuit.data:
-> 1495         _write_instruction(instruction_buffer, instruction, custom_instructions, index_map)
   1496     file_obj.write(struct.pack(CUSTOM_DEFINITION_HEADER_PACK, len(custom_instructions)))
   1497 

~/q/qiskit-terra/qiskit/circuit/qpy_serialization.py in _write_instruction(file_obj, instruction_tuple, custom_instructions, index_map)
   1223         else:
   1224             raise TypeError(
-> 1225                 f"Invalid parameter type {instruction_tuple[0]} for gate {type(param)},"
   1226             )
   1227         instruction_param_raw = struct.pack(INSTRUCTION_PARAM_PACK, type_key.encode("utf8"), size)

TypeError: Invalid parameter type Instruction(name='if_else', num_qubits=1, num_clbits=1, params=[<qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x13c510750>, None]) for gate <class 'qiskit.circuit.quantumcircuit.QuantumCircuit'>,

What should happen?

The circuit should be able to be serialized and de-serialized.

Any suggestions?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mtreinishcommented, Jan 26, 2022

Right, I just meant we have a place already for this kind of thing where we have to do that kind of custom deserialization construction. Since we had to do it for Barrier (which does Barrier(num_qubits) which isn’t a parameter) and Initialize (which does Initialize(params) instead of Initialize(*params). So for 0.19.4 I think if we do something like:

if gate_name in {"IfElseOp", "WhileLoopOp"}:
    gate_class(condition, *params)

around here: https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/circuit/qpy_serialization.py#L986-L992 that fits in with an existing pattern and won’t be too intrusive and seems like the simple path for a backport fix.

As for None, that’s a good call we don’t support that yet either. That’s trivial to add as a supported param type, we can add that at the same time we do QuantumCircuit

0reactions
jakelishmancommented, Jan 26, 2022

Having the condition is fine, the problem is that the constructor of IfElseOp and WhileLoopOp take condition as their first positional argument, and don’t put condition in the params attribute. The condition is the same tuple, but you do something like

my_if = IfElseOp((creg, 1), true_body, false_body)
assert len(my_if.params) == 2
assert my_if.params[0] is true_body
assert my_if.params[1] is false_body

We can do it, but it’ll need special handling for IfElseOp and WhileLoopOp when reconstructing the instances, to pass the condition positionally first (they error if there’s no condition).

Also, can qpy currently serialise a parameter of None? IfElseOp can have None as its second parameter if there isn’t a false body.

Read more comments on GitHub >

github_iconTop Results From Across the Web

QPY serialization ( qiskit.qpy )
Using QPY¶. Using QPY is defined to be straightforward and mirror the user API of the serializers in Python's standard library, pickle and...
Read more >
Go websocket serialization/deserialization json - Stack Overflow
I'm using gorilla websocket, and i'm planing using json for serialization/deserialization. Let say ...
Read more >
FnT - River Thames Conditions - Environment Agency - GOV.UK
Aqw king alteon quest id, Iab advertising guidelines. ... 451 4.4.0 error, Elms farm roxwell essex, How to mix ground fire clay, 120/240...
Read more >
ALBPM Studio Help
If your existing 5.x code fails on version 6 because of this error, ... inside of a Split-Join circuit, this same Grab activity...
Read more >
Java, Java, Java - Computer Science
“objects first” approach to programming and problem solving that was ... set of instructions that tell a computer what to do.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found