Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZX-Calculus pass for Qiskit Terra #27

Closed
IvanIsCoding opened this issue Aug 16, 2022 · 8 comments
Closed

ZX-Calculus pass for Qiskit Terra #27

IvanIsCoding opened this issue Aug 16, 2022 · 8 comments

Comments

@IvanIsCoding
Copy link

Description

This project consists of implementing a ZX Calculus Optimization pass for Qiskit Terra (Qiskit/qiskit#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

  • Code to convert DAGCircuit to a format usable by pyzx (e.g. Graph, Circuit)
  • Code to convert from pyzx format to DAGCircuit

Mentors details

  • Mentor 1
    • Name: Ivan Carvalho
    • GitHub ID: @IvanIsCoding
    • What they do: Rustworkx Developer

Number of mentees

2

Type of mentees

  • Mentee 1
    • Required:
      • Strong Python Programming skills
    • Nice to have:
      • Knowledge of graph theory is a plus
      • Willing to learn ZX Calculus
  • Mentee 2
    • Required:
      • Familiar with ZX Calculus
    • Nice to have:
      • Knowledge of graph theory is a plus
      • Willing to learn Python
@Hirmay
Copy link

Hirmay commented Aug 25, 2022

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

@gprs1809
Copy link

gprs1809 commented Sep 3, 2022

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
Copy link
Author

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
Copy link

@GemmaDawson

@gprs1809
Copy link

gprs1809 commented Oct 7, 2022

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

@gprs1809
Copy link

gprs1809 commented Nov 2, 2022

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
Copy link

gprs1809 commented Dec 12, 2022

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Final Showcase Submitted
Development

No branches or pull requests

6 participants