PennyLaneAI / PennyLaneAI/catalyst
Canonicalization of inverse extract/insert pairs on the same index is obfuscated if there is a chain of inserts
Open
Nobody has claimed this yet.
compiler
good first issue
- Dominant language
- Python
- Stars
- 234
- Forks
- 84
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 66
Description
Consider the circuit CNOT(0, 1) - X(0)
%0 = quantum.alloc( 2) : !quantum.reg
%1 = quantum.extract %0[ 0] : !quantum.reg -> !quantum.bit
%2 = quantum.extract %0[ 1] : !quantum.reg -> !quantum.bit
%out_qubits:2 = quantum.custom "CNOT"() %1, %2 : !quantum.bit, !quantum.bit
%3 = quantum.insert %0[ 0], %out_qubits#0 : !quantum.reg, !quantum.bit
%4 = quantum.insert %3[ 1], %out_qubits#1 : !quantum.reg, !quantum.bit
%5 = quantum.extract %4[ 0] : !quantum.reg -> !quantum.bit
%out_qubits_0 = quantum.custom "PauliX"() %5 : !quantum.bit
%6 = quantum.insert %4[ 0], %out_qubits_0 : !quantum.reg, !quantum.bit
quantum.dealloc %6 : !quantum.reg
After --canonicalize, the IR stays unchanged.
Proposed improvement:
By interchanging the order of the two inserts after the CNOT:
%0 = quantum.alloc( 2) : !quantum.reg
%1 = quantum.extract %0[ 0] : !quantum.reg -> !quantum.bit
%2 = quantum.extract %0[ 1] : !quantum.reg -> !quantum.bit
%out_qubits:2 = quantum.custom "CNOT"() %1, %2 : !quantum.bit, !quantum.bit
%4 = quantum.insert %0[ 1], %out_qubits#1 : !quantum.reg, !quantum.bit
%3 = quantum.insert %4[ 0], %out_qubits#0 : !quantum.reg, !quantum.bit
%5 = quantum.extract %3[ 0] : !quantum.reg -> !quantum.bit
%out_qubits_0 = quantum.custom "PauliX"() %5 : !quantum.bit
%6 = quantum.insert %4[ 0], %out_qubits_0 : !quantum.reg, !quantum.bit
quantum.dealloc %6 : !quantum.reg
we should be able to detect the inverse pair on index 0:
%0 = quantum.alloc( 2) : !quantum.reg
%1 = quantum.extract %0[ 0] : !quantum.reg -> !quantum.bit
%2 = quantum.extract %0[ 1] : !quantum.reg -> !quantum.bit
%out_qubits:2 = quantum.custom "CNOT"() %1, %2 : !quantum.bit, !quantum.bit
%3 = quantum.insert %0[ 1], %out_qubits#1 : !quantum.reg, !quantum.bit
%out_qubits_0 = quantum.custom "PauliX"() %out_qubits#0 : !quantum.bit
%4 = quantum.insert %3[ 0], %out_qubits_0 : !quantum.reg, !quantum.bit
quantum.dealloc %4 : !quantum.reg
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the supplied MLIR circuit and running --canonicalize, then trace the canonicalization handling for quantum.extract and quantum.insert through the chained inserts. Done means the inverse pair at index 0 is detected and the IR is reduced to the form shown in the proposed improvement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100