CQCL / tket

Source code for the TKET quantum compiler, Python bindings and utilities
https://docs.quantinuum.com/tket/
Apache License 2.0
256 stars 48 forks source link

Make `CXMappingPass()` serializable [was: Replace the `RebaseCustom` in `qiskit_convert.py` with a serializable pass] #1573

Closed cqc-alec closed 1 month ago

CalMacCQ commented 2 months ago

Would using AutoRebase work for this? No tk1_replacement function is needed for AutoRebase.

Also is there a reason this needs to be serialized? The tk_to_qiskit function is not used in the default_compilation_pass for any Backends as far as I know.

cqc-alec commented 1 month ago

Would using AutoRebase work for this? No tk1_replacement function is needed for AutoRebase.

Yes I think AutoRebase would work for this (the target gateset is large).

Also is there a reason this needs to be serialized? The tk_to_qiskit function is not used in the default_compilation_pass for any Backends as far as I know.

Good question. I can't see why it would be needed. But @johnchildren mentioned that some of the default passes for ibm-qiskit were not serializable. At the moment I'm struggling to see how a RebaseCustom is making its way into any of these.

cqc-alec commented 1 month ago

@johnchildren Could you provide more details of the non-serializable default pass in pytket-qiskit?

johnchildren commented 1 month ago

@cqc-alec Sure, using this script I get quite a large dict that contains this pass:

{'StandardPass': {'basis_allowed': ['CX', 'GPI2', 'GPI', 'PhasedX', 'TK1', 'Reset', 'U1', 'Collapse', 'U2', 'Measure', 'U3', 'noop', 'Rz', 'Ry', 'Z', 'X', 'Y', 'S', 'Sdg', 'T', 'Tdg', 'V', 'Vdg', 'SX', 'SXdg', 'H', 'Rx'], 'basis_cx_replacement': {'bits': [], 'commands': [{'args': [['q', [0]], ['q', [1]]], 'op': {'type': 'CX'}}], 'created_qubits': [], 'discarded_qubits': [], 'implicit_permutation': [[['q', [0]], ['q', [0]]], [['q', [1]], ['q', [1]]]], 'phase': '0.0', 'qubits': [['q', [0]], ['q', [1]]]}, 'basis_tk1_replacement': 'SERIALIZATION OF FUNCTIONS IS NOT YET SUPPORTED', 'name': 'RebaseCustom'}, 'pass_class': 'StandardPass'}
from pytket.extensions.qiskit.backends.ibm import IBMQBackend

print(IBMQBackend(backend_name="ibm_kyiv").default_compilation_pass().to_dict())

pytket-qiskit version 0.55, pytket version 1.31.1

CalMacCQ commented 1 month ago

Thanks John, should be easy to replace this use of RebaseCustom

CalMacCQ commented 1 month ago

I am slightly puzzled that this RebaseCustom pass shows up in the dict at all. Its not a part of the default_compilation_pass in and of itself. Rather the circuit is converted from pytket -> qiskit after compilation for submission to the wrapped qiskit backend.

cqc-alec commented 1 month ago

Unfortunately CQCL/pytket-qiskit#387 did not fix this: John's script still prints JSON including 'basis_tk1_replacement': 'SERIALIZATION OF FUNCTIONS IS NOT YET SUPPORTED'.

CalMacCQ commented 1 month ago

Yes I also spotted this... Puzzled as to why its showing up as it was the only place that RebaseCustom appeared in the pytket-qiskit extension.

cqc-alec commented 1 month ago

Yes I also spotted this... Puzzled as to why its showing up as it was the only place that RebaseCustom appeared in the pytket-qiskit extension.

I suppose it must be used in tket inside one of the other passes?

CalMacCQ commented 1 month ago

I suppose it must be used in tket inside one of the other passes?

Yes, that must be the reason. I guess we should try and update this internal rebasing if we can?

I noticed that this didn't remove the RebaseCustom pass from the dict before merging CQCL/pytket-qiskit#387. However I still think it was worth doing for the code simplification.

johnchildren commented 1 month ago

To clarify something for me as well, does every Custom pass have a 'basis_tk1_replacement'? It looked like from the serialization code that value would always be returned even if there was no replacement in the pass?

cqc-alec commented 1 month ago

To clarify something for me as well, does every Custom pass have a 'basis_tk1_replacement'? It looked like from the serialization code that value would always be returned even if there was no replacement in the pass?

Every RebaseCustom pass (not every CustomPass) contains a basis_tk1_replacement. However, a pass generated from AutoRebase should not:

from pytket.circuit import OpType
from pytket.passes import AutoRebase

p = AutoRebase(set([OpType.Rx, OpType.Rz, OpType.CX]))
print(p.to_dict())
{'StandardPass': {'allow_swaps': False,
  'basis_allowed': ['Rx', 'Rz', 'CX'],
  'name': 'AutoRebase'},
 'pass_class': 'StandardPass'}
CalMacCQ commented 1 month ago

Perhaps this issue should be closed now. Maybe we should open a follow-up on the CQCL/tketrepo?

cqc-alec commented 1 month ago

Let's keep it open for now; it needs more investigation before we know where the fix should be.

cqc-alec commented 1 month ago

The non-serializable component of the pass is the CXMappingPass(). The fix for this should indeed be in TKET, so I will move this issue to the tket repo (and rename it).