TeamGraphix / graphix

measurement-based quantum computing (MBQC) compiler and simulator
https://graphix.readthedocs.io
Apache License 2.0
60 stars 21 forks source link

Organize the visualization #119

Closed d1ssk closed 6 months ago

d1ssk commented 7 months ago

Before submitting, please check the following:

Then, please fill in below:

Context (if applicable):

Description of the change:

Related issue:

118

also see that checks (github actions) pass. If lint check keeps failing, try installing black==22.8.0 as behavior seems to vary across versions.

d1ssk commented 7 months ago

some examples

スクリーンショット 2024-02-09 18 27 54
shinich1 commented 7 months ago

I have a related (but not directly about the commits made here) comment: https://github.com/TeamGraphix/graphix/blob/be854f70a5f06859d2d794eee5b57a8dc89b6f4e/graphix/visualization.py#L100-L103 It looks like by default we do not search for gflow when calling pattern.draw_graph but still say gflow found. If I'm not mistaken, gflow_from_pattern returns gflow-like structure even if there's no gflow in the graph.

What would you say about sorting out this part a little, such that

cc: @masa10-f

d1ssk commented 7 months ago

My plan is as follows:

Here, xflow and zflow refer to collections of correction signals of X and Z, respectively, within the pattern.

d1ssk commented 7 months ago

These images show examples of visualization outcomes. The visualization approach for flow/gflow remains largely unchanged, except for a modification in the arrow style for flow. The detection of flow/gflow within patterns has become rigorous. In cases without flow/gflow, the nodes are neatly arranged. Moreover, when visualizing from a pattern, both xflow and zflow are also depicted.

スクリーンショット 2024-02-29 21 17 04 スクリーンショット 2024-02-29 21 17 13
d1ssk commented 7 months ago

The flow_from_pattern and gflow_from_pattern methods consistently produced accurate results when testing patterns generated from graphs using generator.py. However, I observed that most patterns transpiled from circuits are not consistent with flow or gflow, resulting in that most visualizations within the examples are with red and green arrows. (I added flow_from_pattern argument in draw_graph so that we can get the previous result when setting it False)

I suspect that the majority of patterns derived from circuits is in Pauli flow structure rather than flow or gflow. I plan to develop a pauliflow_from_pattern method in PR #117 to investigate this hypothesis.

masa10-f commented 7 months ago

However, I observed that most patterns transpiled from circuits do not flow or gflow.

Can you show an example of flow-absent pattern transpiled from circuit? Generally, pattern from circuit must have flow, so it sounds strange to me...

masa10-f commented 7 months ago

Or, you visualized a pattern after shift_signals ? It can change the original flow into gflow or Pauli flow. Can you visualize a pattern after standardize but before shift_signals ?

d1ssk commented 7 months ago

Like this. Actually it's obvious from figure that the correction flow structure is not flow. The graph itself "has" flow structure of course, but the pattern sequence is not consistent with that. I checked that xflow and zflow here is consistent with pattern.seq.

スクリーンショット 2024-02-29 22 02 40

I haven't thoroughly checked the implementation of the transpiler yet, and perhaps this is because some default optimization in the transpiler?

masa10-f commented 7 months ago

Thanks!

this is because some default optimization in the transpiler?

I agree with you. For example, the following code is Ry gate, but feedforward structure is different from flow.

It seems that we should have pauliflow_from_pattern to visualize a pattern in practical use.

  def _ry_command(self, input_node: int, ancilla: Sequence[int], angle: float):
      """MBQC commands for Y rotation gate

      Parameters
      ---------
      input_node : int
          input node index
      ancilla : list of four ints
          ancilla node indices to be added to graph
      angle : float
          rotation angle in radian

      Returns
      ---------
      out_node : int
          control node on graph after the gate
      commands : list
          list of MBQC commands
      """
      assert len(ancilla) == 4
      seq = [["N", ancilla[0]], ["N", ancilla[1]]]  # assign new qubit labels
      seq.extend([["N", ancilla[2]], ["N", ancilla[3]]])
      seq.append(["E", (input_node, ancilla[0])])
      seq.append(["E", (ancilla[0], ancilla[1])])
      seq.append(["E", (ancilla[1], ancilla[2])])
      seq.append(["E", (ancilla[2], ancilla[3])])
      seq.append(["M", input_node, "XY", 0.5, [], []])
      seq.append(["M", ancilla[0], "XY", -1 * angle / np.pi, [input_node], []])
      seq.append(["M", ancilla[1], "XY", -0.5, [input_node], []])
      seq.append(["M", ancilla[2], "XY", 0, [], []])
      seq.append(["X", ancilla[3], [ancilla[0], ancilla[2]]])
      seq.append(["Z", ancilla[3], [ancilla[0], ancilla[1]]])
      return ancilla[3], seq
d1ssk commented 7 months ago

Thanks! I'm not familiar with transpile algorithm yet and might be mistaken, but do you think it's possible to first construct a pattern without correction assuming that all the measurement outcome is 0, and after that, modify the commands to integrate corrections according to flow or gflow of the graph? If so, I guess it's even possible to get minimal-correction pattern.

shinich1 commented 7 months ago

@d1ssk Nice work!! I think a dedicated example or tutorial page would be nice to show the range of visualizer capability. I am happy to write it up once the discussion above are resolved, or if you're keen feel free to add one.

unrelated note - we should update the documentation page for more modern look and it needs better-organized tutorial/API ref. will think about this later and create an issue.

masa10-f commented 7 months ago

do you think it's possible to first construct a pattern without correction assuming that all the measurement outcome is 0, and after that, modify the commands to integrate corrections according to flow or gflow of the graph?

Yes, it's possible. But, I heard that currently perform_pauli_measurments violates it somehow. So, we might have to address this problem when implementing it. Or, it would be more natural to express a correction-less pattern in graph. I believe it would make better UI to separate measurement-error correction(flow or signals) from Unitary embedding(angle, planes, and graph structures).

d1ssk commented 7 months ago

@shinich1 This discussion is not directly related to this PR and I will tackle it in other PR. Could you please go ahead to make the documentation page? Thanks.

shinich1 commented 7 months ago

@d1ssk could you write a few lines in the changelog? also the xflow and zflow etc legends are cut off when showing in the gallery, see here. Perhaps @nabe98 could help with this?

after these are fixed, let us squash and merge.