0
$\begingroup$

It is possible to create an ASCII graph in Stimzx for a ZX calculus diagram. I would like to create something like this XX parity measurement

but I cannot seem to recreate the correct ASCII format. When I do something like this

stimzx.text_diagram_to_zx_graph("""
        in---X---out
              \
               Z(pi)
              /
        in---X---out
    """)

I get complaints like

KeyError                                  Traceback (most recent call last)
<ipython-input-57-09740d1c5674> in <module>
      5              /
      6        in---X---out
----> 7    """))

c:~\_zx_graph_solver.py in text_diagram_to_zx_graph(text_diagram)
     89             reading ordering from the diagram, and have a "value" attribute of type `stimzx.ZxType`.
     90     """
---> 91     return text_diagram_to_networkx_graph(text_diagram, value_func=ZX_TYPES.__getitem__)
     92 
     93 

My questions:

  1. What is incorrect in my ASCII syntax?
  2. Is there an easier way to import ZX graphs e.g. is there any interoperability with the PyZX format?
$\endgroup$

1 Answer 1

1
$\begingroup$

What is incorrect in my ASCII syntax?

The backslash \ is being treated as an escape character, resulting in this being the actual text given to the method:

        in---X---out
                             Z(pi)
              /
        in---X---out

The KeyError message is a bad error message caused by the edge to nowhere.

You can use a raw string (prefix the quotes with r) to prevent this:

#                             look!
#                               v
#                               v
#                               v
stimzx.text_diagram_to_zx_graph(r"""
        in---X---out
              \
               Z(pi)
              /
        in---X---out
    """)

Is there an easier way to import ZX graphs e.g. is there any interoperability with the PyZX format?

The text diagram method produces an annotated networkx graph, which is what's actually used by later methods. You can produce that annotated graph directly, instead of going through the text route. StimZX was basically a weekend project done for a QPL demo, so it has very few bells and whistles... I'd be happy to merge a contribution that added a method to load a graph from PyZX.


from typing import List

import stimzx
import networkx

g: networkx.Graph = stimzx.text_diagram_to_zx_graph(r"""
        in---X---out
              \
               Z(pi)
              /
        in---X---out
    """)

s: List[stimzx.ExternalStabilizer] = stimzx.zx_graph_to_external_stabilizers(g)
for e in s:
    print(e)

+XX -> +__
+ZZ -> +ZZ
+_X -> +_X
+__ -> +XX

Yup that's an X basis measurement.

$\endgroup$
3
  • $\begingroup$ Thanks Craig. The output is suprising me a bit insofar as I expect to see a minus on the RHS somewhere (the -1 outcome of the XX measurement). To be clear, the external stabilizers for the same ZX graph with a plain Z node (as opposed to Z(pi)) are exactly the same. A simpler manifestation of this sign confusion would be that X---out and X(pi)---out both give "+ -> +Z" as external stabilizers, whereas I would expect the second graph to prepare the $\vert 1 \rangle$ state, which has -Z as a stabilizer. $\endgroup$
    – Mark
    Commented Jun 26, 2023 at 13:44
  • $\begingroup$ @Mark I think you're right that it's supposed to have a minus sign. In the link that I gave I accidentally didn't use a pi node; adding it shows the minus signs. It looks like the stimzx code is getting the sign wrong. $\endgroup$ Commented Jun 26, 2023 at 16:16
  • 1
    $\begingroup$ @Mark should be fixed by github.com/quantumlib/Stim/pull/595 $\endgroup$ Commented Jul 19, 2023 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.