tqec icon indicating copy to clipboard operation
tqec copied to clipboard

Steane Example in PyZX

Open KabirDubey opened this issue 8 months ago • 26 comments

We need a PyZX graphS object that represents the Steane example from these slides (e.g. slide 59). Please let me know if we already have one.

In an initial attempt, I was able to read a qasm file, but input edges must be closed. In the steane_code.qasm file, I wrote out the entire Steane example circuit, but PyZX was unable to parse my reset instructions.

EDIT: To avoid confusion, I renamed this issue and deleted broader questions about PyZX interoperability. Broader discussions about interoperability can happen at #449.

KabirDubey avatar Mar 19 '25 18:03 KabirDubey

I wrote out the entire Steane example circuit, but PyZX was unable to parse my reset instructions.

The workaround to this is to write the whole circuit in pyzx format. I have got an output after applying all the rules but I need to further simplify it to get it to exactly look like Austin's slide 59.

import pyzx as zx
from pyzx.circuit import Circuit, CNOT, HAD

c = zx.Circuit(10)


# qubits 0, 1, 2 are ancillas

c.add_gate("HAD", 0)
c.add_gate("CNOT", 0, 3)
c.add_gate("CNOT", 0, 4)
c.add_gate("CNOT", 0, 5)
c.add_gate("CNOT", 0, 6)
c.add_gate("HAD", 0)
c.add_gate("PostSelect", 0)


c.add_gate("HAD", 1)
c.add_gate("CNOT", 1, 3)
c.add_gate("CNOT", 1, 4)
c.add_gate("CNOT", 1, 7)
c.add_gate("CNOT", 1, 8)
c.add_gate("HAD", 1)
c.add_gate("PostSelect", 1)

c.add_gate("HAD", 2)
c.add_gate("CNOT", 2, 3)
c.add_gate("CNOT", 2, 5)
c.add_gate("CNOT", 2, 7)
c.add_gate("CNOT", 2, 9)
c.add_gate("HAD", 2)
c.add_gate("PostSelect", 2)



g = c.to_graph()
g.apply_state('0'*10)
zx.draw(g)

purva-thakre avatar Mar 19 '25 18:03 purva-thakre

@KabirDubey Did you get a chance to work on this yet? I am trying to find a way to be consistent with how rules are applied in pyzx compared to Austin's slides.

The issue is that if I use the rules in pyzx rules as they are, the edge colors are changed whereas we want the node color to change.

purva-thakre avatar Mar 22 '25 21:03 purva-thakre

@KabirDubey Did you get a chance to work on this yet?

Not yet, I was planning on working on this later today or tomorrow. I'm broadly available today onwards if you or anyone else would like to discuss offline.

The issue is that if I use the rules in pyzx rules as they are, the edge colors are changed whereas we want the node color to change.

I see. I saw a similar phenomenon when I was playing around with PyZX on my branch. Part of my vision for this issue is to document how the existing functionality of PyZX can be used for tqec.

KabirDubey avatar Mar 22 '25 22:03 KabirDubey

I'm broadly available today onwards if you or anyone else would like to discuss offline.

Probably not today or tomorrow.

purva-thakre avatar Mar 22 '25 22:03 purva-thakre

Hi @KabirDubey and @purva-thakre, you might need to interpret the boundary node's Pauli web in PyZX differently. Aleks, Austin and I are in email conversations regarding that.

A separate issue: I have also emailed Adrien about a bug in TQEC, if we start purely from a ZX diagram, the correlation surface finder seems to be buggy, or at least the plotting of Pauli webs (or maybe I am doing something wrong).

Image

kh428 avatar Mar 24 '25 12:03 kh428

you might need to interpret the boundary node's Pauli web in PyZX differently

@kh428 Thanks! I am working to understand this. Just to be clear, a Pauli web in PyZX is the same as a correlation surface in tqec, right?

purva-thakre avatar Mar 24 '25 21:03 purva-thakre

Right :-)

On Mon, Mar 24, 2025 at 2:38 PM Purva Thakre @.***> wrote:

you might need to interpret the boundary node's Pauli web in PyZX differently

@kh428 https://github.com/kh428 Thanks! I am working to understand this. Just to be clear a Pauli web in PyZX is the same as a correlation surface in tqec, right?

— Reply to this email directly, view it on GitHub https://github.com/tqec/tqec/issues/528#issuecomment-2749460037, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKAXTC35LJM7UOMVAT2V5D2WB3OHAVCNFSM6AAAAABZLQLLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONBZGQ3DAMBTG4 . You are receiving this because you are subscribed to this thread.Message ID: @.***> [image: purva-thakre]purva-thakre left a comment (tqec/tqec#528) https://github.com/tqec/tqec/issues/528#issuecomment-2749460037

you might need to interpret the boundary node's Pauli web in PyZX differently

@kh428 https://github.com/kh428 Thanks! I am working to understand this. Just to be clear a Pauli web in PyZX is the same as a correlation surface in tqec, right?

— Reply to this email directly, view it on GitHub https://github.com/tqec/tqec/issues/528#issuecomment-2749460037, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKAXTC35LJM7UOMVAT2V5D2WB3OHAVCNFSM6AAAAABZLQLLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONBZGQ3DAMBTG4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

afowler avatar Mar 24 '25 21:03 afowler

I am trying to find a way to be consistent with how rules are applied in pyzx compared to Austin's slides.

To answer my own question on this, the current set of Hadamard rules in pyzx does not allow this.

https://github.com/zxcalc/pyzx/blob/64ff4594a5104851ab7ccdb492e72548c4f10b27/pyzx/hrules.py#L4

you might need to interpret the boundary node's Pauli web in PyZX differently

@kh428 Do you mind clarifying this further? Based on the pyzx documentation about Pauli web, if a certain number of conditions are not satisfied by a node (spider) or edge, then this node or edge is called the boundary. How would I interpret this differently?

https://github.com/zxcalc/pyzx/blob/64ff4594a5104851ab7ccdb492e72548c4f10b27/pyzx/pauliweb.py#L54

purva-thakre avatar Mar 26 '25 23:03 purva-thakre

There is only one Hadamard rule. Choose any node in the ZX graph, you can flip its color and flip the presence of Hadamards on its edges.

On Wed, Mar 26, 2025, 4:32 PM Purva Thakre @.***> wrote:

I am trying to find a way to be consistent with how rules are applied in pyzx compared to Austin's slides.

To answer my own question on this, the current set of Hadamard rules in pyzx does not allow this.

https://github.com/zxcalc/pyzx/blob/64ff4594a5104851ab7ccdb492e72548c4f10b27/pyzx/hrules.py#L4

you might need to interpret the boundary node's Pauli web in PyZX differently

@kh428 https://github.com/kh428 Do you mind clarifying this further? Based on the pyzx documentation about Pauli web, if a certain number of conditions are not satisfied by a node (spider) or edge, then this node or edge is called the boundary. How would I interpret this differently?

https://github.com/zxcalc/pyzx/blob/64ff4594a5104851ab7ccdb492e72548c4f10b27/pyzx/pauliweb.py#L54

— Reply to this email directly, view it on GitHub https://github.com/tqec/tqec/issues/528#issuecomment-2755997529, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKAXTHMWHJJ2FL7LAVOXFD2WM2IFAVCNFSM6AAAAABZLQLLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONJVHE4TONJSHE . You are receiving this because you commented.Message ID: @.***> [image: purva-thakre]purva-thakre left a comment (tqec/tqec#528) https://github.com/tqec/tqec/issues/528#issuecomment-2755997529

I am trying to find a way to be consistent with how rules are applied in pyzx compared to Austin's slides.

To answer my own question on this, the current set of Hadamard rules in pyzx does not allow this.

https://github.com/zxcalc/pyzx/blob/64ff4594a5104851ab7ccdb492e72548c4f10b27/pyzx/hrules.py#L4

you might need to interpret the boundary node's Pauli web in PyZX differently

@kh428 https://github.com/kh428 Do you mind clarifying this further? Based on the pyzx documentation about Pauli web, if a certain number of conditions are not satisfied by a node (spider) or edge, then this node or edge is called the boundary. How would I interpret this differently?

https://github.com/zxcalc/pyzx/blob/64ff4594a5104851ab7ccdb492e72548c4f10b27/pyzx/pauliweb.py#L54

— Reply to this email directly, view it on GitHub https://github.com/tqec/tqec/issues/528#issuecomment-2755997529, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKAXTHMWHJJ2FL7LAVOXFD2WM2IFAVCNFSM6AAAAABZLQLLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONJVHE4TONJSHE . You are receiving this because you commented.Message ID: @.***>

afowler avatar Mar 27 '25 01:03 afowler

I am trying to find a way to be consistent with how rules are applied in pyzx compared to Austin's slides.

Re: "consistent with how rules are applied in PyZX compared to Austin's slides", what do you mean, sorry I don't understand? My understanding is that they are consistent? You have the option of choosing between a Hadamard edge (cyan coloured) or an actual Hadamard vertex in PyZX.

Re: Using PyZX to finding the Pauli webs. Let's look at the Steane code encoding logical circuit below. PyZX cannot output Pauli webs because the graph does not have gflow (generalised flow). Image

However if you leave the output ports open (see below), Image Then you can compute Pauli webs in PyZX. If you look at the Pauli-Z webs computed by PyZX then, you will notice something strange (see below). Image The vertex labelled 0 has a 1 same colour Pauli web terminating on it.

Referring to https://arxiv.org/abs/2410.17240, Pauli webs can be thought of as firing of spiders, i.e. choose a node and by adding pi spiders in the vicinity that respects the local symmetry of the particular node. Image Image

To quote Aleks Kissinger: "Boundary spiders, where the coloured edges do not correspond to an (anti-)stabiliser for that spider. These correspond to locations where a local, quantum correction might be needed (as opposed to just classical post-processing)."

This is what I interpreted that the Pauli webs has a modified interpretation when there are boundary spiders. Let me know if I have made any mistakes/misinterpreted anything.

kh428 avatar Mar 27 '25 10:03 kh428

Re: "consistent with how rules are applied in PyZX compared to Austin's slides", what do you mean, sorry I don't understand? My understanding is that they are consistent? You have the option of choosing between a Hadamard edge (cyan coloured) or an actual Hadamard vertex in PyZX.

https://docs.google.com/presentation/d/184GHX9jffq9dcwWzbku0K90V9xdA8_25lD8pfeEcgRg/edit#slide=id.g334ab20f3f2_0_190

See slides 51 and 52.

Image

Image

@kh428 It's the rule where the color of a vertex is changed: (quoting Austin) if every edge of a node is a Hadamard edge, you can remove the Hadamards and change the color of the node.

I can't seem to find anything similar to this in pyzx/hrules.py. I have either an H edge or an H vertex in my graph, and that's it.

Image

Image

I may not be interpreting some function docstrings correctly. Do you mind pointing me to a function like this in pyzx? My understanding so far is that I need to define a custom rule in my jupyter notebook to go from Slides 51 to 52.

Thank you for your helpful comment!

purva-thakre avatar Mar 27 '25 15:03 purva-thakre

@purva-thakre sorry, I understand you now. I am not sure how, have you looked at pyzx/hsimplify.py, maybe there is something in there?

kh428 avatar Mar 27 '25 15:03 kh428

I am not sure how, have you looked at pyzx/hsimplify.py, maybe there is something in there?

Thanks @kh428. Austin reached out to Aleks, and apparently I was looking in the wrong module. hrules.py is for the ZH-Calculus.

purva-thakre avatar Apr 09 '25 17:04 purva-thakre

It looks like pyzx might need some additional work for us to go from Slides 51 - 59.

The PostSelect gate is represented by the Z vertex, whereas it should be the X vertex. If I use the pyzx rules on this, the output looks something like this:

Image

There are issues with the Measurement gate not being supported in QASM circuit to pyzx circuit functionality. As noticed by Kabir.

I also tried starting with a graph instead of converting a circuit to a graph, as it allows more control over measurement ~ X node. This output is a mess. 0, 1, and 2 are ancillas.

Image

Applying some of the pyzx rules, this is the closest I have gotten to the desired output.

Image

Edit: There was an error in the previous graph where a Z node was in place of an X node. The updated graphs are provided below.

Image

Image

After removing the identity nodes, I get the desired output:

Image

purva-thakre avatar Apr 09 '25 18:04 purva-thakre

Thanks @purva-thakre, in the final graph, can you check if you could compute the pauli webs?

kh428 avatar Apr 09 '25 20:04 kh428

@kh428 Computing the pauli webs for the final graph leads to ValueError: Graph must have gFlow

I will clean up my code and provide a link to it by next week.

purva-thakre avatar Apr 10 '25 18:04 purva-thakre

@kh428 Computing the pauli webs for the final graph leads to ValueError: Graph must have gFlow

It seems like one would need to go through PyZX gflow subroutine to compute Pauli webs.

However in the equivalent Pipe/Block diagram in TQEC, the correlation surface finder is able to find correlation surfaces, I suppose. Why this difference?

kh428 avatar Apr 10 '25 19:04 kh428

It seems like one would need to go through PyZX gflow subroutine to compute Pauli webs.

However in the equivalent Pipe/Block diagram in TQEC, the correlation surface finder is able to find correlation surfaces, I suppose. Why this difference?

TBH, I don't know the answer to this. Maybe, @afowler @nelimee @inmzhang do?

purva-thakre avatar Apr 10 '25 20:04 purva-thakre

It seems like one would need to go through PyZX gflow subroutine to compute Pauli webs.

However in the equivalent Pipe/Block diagram in TQEC, the correlation surface finder is able to find correlation surfaces, I suppose. Why this difference?

The last ZX diagram provided by Purva lacks a gflow (likely because it has a different number of inputs and outputs). The Pauli web computation in pyzx directly depends on the gflow property, which is why it fails in this case.

In TQEC, the Pauli web computation doesn't rely on gflow. Instead, it uses local rules for the Pauli web around a spider. For example, a red Pauli spider must have an even number of red-highlighted legs or have no/all legs highlighted in blue. After applying these local rules, a flood-fill algorithm is used to identify the global Pauli webs.

inmzhang avatar Apr 11 '25 01:04 inmzhang

In TQEC, the Pauli web computation doesn't rely on gflow. Instead, it uses local rules for the Pauli web around a spider. For example, a red Pauli spider must have an even number of red-highlighted legs or have no/all legs highlighted in blue. After applying these local rules, a flood-fill algorithm is used to identify the global Pauli webs.

I understand the concept of Pauli webs and it seems like generating it locally and propagating it to the whole ZX graph/structure would be a better approach? How can we rely on PyZX if it cannot even generate the Pauli web for a simple Steane code encoding circuit?

kh428 avatar Apr 11 '25 08:04 kh428

How can we rely on PyZX if it cannot even generate the Pauli web for a simple Steane code encoding circuit?

This is why we do not use the compute_pauli_web function from PyZX in the current codebase. We prefer to use it in the future—likely after PyZX upgrades the algorithm to support more general ZX diagrams—because it also outputs the T orders, which cannot be computed (or at least, I don’t know how to) using a simple graph traversal algorithm.

inmzhang avatar Apr 11 '25 09:04 inmzhang

likely after PyZX upgrades the algorithm to support more general ZX diagrams

Are there any indications that this will happen?

kh428 avatar Apr 11 '25 10:04 kh428

To be clear, I'll think our community is going to have to play a significant role in the future development of PyZX.

On Fri, Apr 11, 2025, 3:36 AM kh428 @.***> wrote:

likely after PyZX upgrades the algorithm to support more general ZX diagrams

Is there any indications that this will indeed happen?

— Reply to this email directly, view it on GitHub https://github.com/tqec/tqec/issues/528#issuecomment-2796530199, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKAXTCZPDJ5AMDHBEGYRXT2Y6LKDAVCNFSM6AAAAABZLQLLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOOJWGUZTAMJZHE . You are receiving this because you were mentioned.Message ID: @.***> kh428 left a comment (tqec/tqec#528) https://github.com/tqec/tqec/issues/528#issuecomment-2796530199

likely after PyZX upgrades the algorithm to support more general ZX diagrams

Is there any indications that this will indeed happen?

— Reply to this email directly, view it on GitHub https://github.com/tqec/tqec/issues/528#issuecomment-2796530199, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKAXTCZPDJ5AMDHBEGYRXT2Y6LKDAVCNFSM6AAAAABZLQLLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOOJWGUZTAMJZHE . You are receiving this because you were mentioned.Message ID: @.***>

afowler avatar Apr 11 '25 13:04 afowler

To quote this paper here by Mhalla, Perdrix and Sanselme.

"GFlow fails to be necessary for determinism when a measurement-based quantum computation involves Pauli measurements"

and

"In terms of determinism, Pauli measurements satisfy some particular properties that are not captured by GFlow. For instance, an MBQC involving an X measurement can be deterministic, even though the corresponding open graph has no GFlow..."

I guess you can sometime still draw Pauli webs/Stabiliser flow/etc in light of the ZX-diagram having no GFlow. The Shadow Pauli Flow introduced in this paper "provides necessary and sufficient for robust determinism".

kh428 avatar Apr 12 '25 13:04 kh428

Do we have a program implementing the canonical ZX graph version of the quantum circuit that prepares a 7 qubit Steane code. (slide 51)?

Zhaoyilunnn avatar Sep 16 '25 05:09 Zhaoyilunnn

See https://github.com/tqec/tqec/issues/560#issuecomment-2825067525

I had to manually construct it, unfortunately :(

purva-thakre avatar Sep 16 '25 05:09 purva-thakre