qiskit-advocate / qamp-fall-22

Qiskit advocate mentorship program (QAMP) fall 22 cohort (Sep - Dec 2022)
15 stars 7 forks source link

ZX-Calculus pass for Qiskit Terra #27

Closed IvanIsCoding closed 1 year ago

IvanIsCoding commented 1 year ago

Description

This project consists of implementing a ZX Calculus Optimization pass for Qiskit Terra (https://github.com/Qiskit/qiskit-terra/issues/4990). Previously, there had been attempts of integrating ZX Calculus to Qiskit via the pyzx library. However, the conversion relied on converting the representations to QASM, which is not viable option.

Instead, this project will opt to directly convert the data structures from Qiskit (directed acyclic graphs backed by rustworkx) to the data structures of pyzx.

Deliverables

Mentors details

Number of mentees

2

Type of mentees

Hirmay commented 1 year ago

Hi Ivan, the project seems pretty interesting to me. Can you point to some references for more details regarding the project?

gprs1809 commented 1 year ago

Hello @IvanIsCoding, this project looks super interesting. I have opted for this. I have been wanting to learn and understand ZX calculus and when I saw this, I thought this would be a good opportunity. I am well-versed with Python. I have studied a lot of Graph Theory during my education. Looking forward to a discussion and hopefully, get an opportunity to work on this :)

IvanIsCoding commented 1 year ago

Hello @IvanIsCoding, this project looks super interesting. I have opted for this. I have been wanting to learn and understand ZX calculus and when I saw this, I thought this would be a good opportunity. I am well-versed with Python. I have studied a lot of Graph Theory during my education. Looking forward to a discussion and hopefully, get an opportunity to work on this :)

I requested to have you working on this, thanks for letting me know it is your first option

Irnamosa commented 1 year ago

@GemmaDawson

gprs1809 commented 1 year ago

This is the checkpoint 1 presentation that we presented yesterday :) ZX_calculus_pass_for_Qiskit_Terra.pptx

gprs1809 commented 1 year ago

Checkpoint 2

After understanding ZX Calculus from the main paper, we dived deeper into pyZX. We have implemented the part of our project that involves converting ZX-diagrams into Qiskit DAG circuits.

pyZX offers a Graph API and a Circuit API. We observe 2 cases most relevant for us and these cases are also more common:

1) When we have a pyZX circuit, in which case we could directly use pyZX built-in optimization called full_optimize. The pyZX Circuit API helps us extract gates in the pyZX format which can be then mapped to Qiskit circuit gates. 2) We have a ZX graph and we can extract circuit from it, using pyZX’s extract_circuit method. This is again followed by full_optimize and mapping of gates between pyZX and Qiskit.

There are other cases which we have also tried incorporating. One such case is when extract_circuit does not work for ZX graphs. In this case, we use full_reduce optimization offered by pyZX. The full_reduce method, while optimizing also turns all the red-spiders into green ones. The Graph API helps us with extracting phases of each green spider, the edges, the input and output vertices, most importantly the qubit mapping between each vertex and the qubits, the edge type which could indicate if there is CZ gate or CNOT gate if the edge is between vertices on 2 different qubits and if there is a H gate when the edge is between vertices on the same qubit. In case the full_reduce optimization gives us vertices that are not mapped on any qubit, we skip full_reduce, use to_gh for conversion into all green spiders and use the same approach we use when full_reduce worked, but this is a rare case for us. At the moment, we are not supporting cases if any 3 or more qubit gates cannot be decomposed into 2-qubit and single qubit gates. In case multi-qubit gates can be decomposed into 2 qubits and single qubit gates, we recommend passing the circuit or graph after the decomposition.

Our implementation so far for ZX to DAG, can be seen here.

One can read more about full_optimize and full_reduce from the pyZX documentation that also provides links to useful papers. Specifically, these approaches use ZX-Calculus concepts like phase polynomials and their optimization, local complementation, pivoting, Clifford simplification, phase gadgets.

Our next step is to focus on unit-testing. We are currently thinking about some common and some unique circuits to test on. Here's an example of how the ZX diagram to Qiskit DAG circuit conversion looks like (the left is a ZX diagram and the right is the corresponding Qiskit DAG circuit):

image image

gprs1809 commented 1 year ago

Final showcase slides: ZX-calculus pass for Qiskit Terra.pptx

My github repo that has the codes: https://github.com/gprs1809/ZX_to_DAG_QAMP_fall_2022

My personal blog describing briefly what I understood about ZX-calculus, transpiler pass, pyZX and my overall QAMP experience: https://medium.com/@grishmaprs/zx-calculus-qiskit-transpiler-pass-pyzx-and-my-qamp-fall-2022-experience-cf17a6731cda

We haven't made a PR yet because we would first resolve the issue we raised on pyZX, but I hope we raise a PR soon and it gets merged with Qiskit-Terra :)

GemmaDawson commented 1 year ago

Congratulations on completing all the requirements for QAMP Fall 2022!! 🌟🌟🌟