QPY serialization fails for circuits with IfElse instructions
See original GitHub issueEnvironment
- 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:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 doesBarrier(num_qubits)
which isn’t a parameter) andInitialize
(which doesInitialize(params)
instead ofInitialize(*params)
. So for 0.19.4 I think if we do something like: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
Having the condition is fine, the problem is that the constructor of
IfElseOp
andWhileLoopOp
takecondition
as their first positional argument, and don’t putcondition
in theparams
attribute. The condition is the same tuple, but you do something likeWe can do it, but it’ll need special handling for
IfElseOp
andWhileLoopOp
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 haveNone
as its second parameter if there isn’t a false body.