Skip to content

Comments

Draft: MBQC Module Refactor with ZX Calculus#2

Closed
masa10-f wants to merge 387 commits intomasterfrom
mf
Closed

Draft: MBQC Module Refactor with ZX Calculus#2
masa10-f wants to merge 387 commits intomasterfrom
mf

Conversation

@masa10-f
Copy link
Collaborator

@masa10-f masa10-f commented Jun 26, 2024

TODO

  • graph state module
    • Abstract Graph State(define API)
    • ZXGraphState(pyzx implementation) Will be implemented by ourselves in another issue bacause it's not the fundamental functionality in MBQC
      • [ ] cover API
      • optimization
    • MBQCCircuit(Unitary Construction)
      • XY-only graph state
      • YZ (and XZ) implementation
  • flow module
    • flow and gflow (use fastflow instead)
    • focusing
    • xflow and zflow for non-deterministic MBQC
  • transpiler module
    • incorporate extended MBQC(see transpiler.generate_m_cmd)
    • standardized pattern(transpiler.transpiler)
    • minimized pattern
  • command module
    • measurement command(migrated from the original repo)
    • pattern definition(will be done in original repo?)
  • pattern and circuit simulator(will be migrated from the original repo)
  • Introduce more strict setting of ruff
  • Write up docsting in numpy style
  • Set up mypy CI
  • Update data type with collections module
  • add more test

Ref:

  • Backens, Miriam, Hector Miller-Bakewell, Giovanni de Felice, Leo Lobski, and John van de Wetering. 2021. “There and Back Again: A Circuit Extraction Tale.” Quantum 5 (421): 421.

@masa10-f
Copy link
Collaborator Author

masa10-f commented Jul 12, 2024

Test Status

  • pattern object
  • circuit object
  • graph state object
  • statevec sim
  • pattern sim
  • circ pattern comparison check
  • transpiler
    • standardized
    • focused(checked focus property but not in a pattern form)
    • reduce space(pattern concatenation is required. I'm tackling it now)

Note

This repo is currently focusing on refactoring methods related to the pattern module in graphix. The resource optimization itself is out of scope now although this repo provides natural interface(GraphState) for it(Pauli preprocessing or ZX calculus). Currenlty I think It might be better to create ZX wrapper after the above TODO list is completed and merged into graphix.

@masa10-f
Copy link
Collaborator Author

masa10-f commented Jul 13, 2024

memo: plan to create mutable and immutable pattern. The former has a measurement calculus method as in the original repo. The latter is fixed pattern generated from the new transpiler.

@masa10-f
Copy link
Collaborator Author

I have refactored pattern object. Now it splits into two steps. One is ImmutablePattern that is passed to simulator or executor. We can not modify it. The other is MutablePattern, which is the basically same as the Pattern in the original repo. I also made an abstract class BasePattern which determinies shared APIs.

I have noticed that add method is not guaranteed to preserve runnability and determinicity of the pattern.

@masa10-f
Copy link
Collaborator Author

Next, change data type of domain list[int] -> set[int] and input/output_nodes list[int] -> set[int]. I also plan to create BasePattern.q_indices variable to record the qargs, which currently preserved as indices of list but I believe it's not good practice to record two differenet information in a single object.

@masato-fuk
Copy link
Contributor

memo: is minimize space necessary in mutable pattern?

@masa10-f
Copy link
Collaborator Author

Next, change data type of domain list[int] -> set[int] and input/output_nodes list[int] -> set[int]. I also plan to create BasePattern.q_indices variable to record the qargs, which currently preserved as indices of list but I believe it's not good practice to record two differenet information in a single object.

I have implemented this. It is probably necessary to add more tests to ensure it. Anyway, I proceed to test space reduction methods in resouce_opt.py

@masa10-f
Copy link
Collaborator Author

I proceed to test space reduction methods in resouce_opt.py

I have made it work and added example to generate reduced space pattern decomposed subgraph sequence. It seems to need a little refactoring. Next add an example of signal shifted pattern generation

@masa10-f
Copy link
Collaborator Author

masa10-f commented Jul 25, 2024

Next things to do

  • implement phase gadget in circuit
  • Macro gate module in circuit class. This includes common gates in circuit libraries(e.g. Rx, Rz, Ry, U3, H etc.)
  • Local Clifford on output vertices
  • Graph State visualizer(unify pattern visualizer in the main repo. there's no need to specify the type of flows.)
  • Macros to convert graph state into pattern easily, for those who familiar with measurement calculus seems not necessary
  • Consider input format from pyzx(necessary info. is graph with phases) we will host ZX Calculus ourselves

@masa10-f
Copy link
Collaborator Author

masa10-f commented Aug 17, 2024

As for local Clifford on outputs, Euler decompisition seems the most natural way on the perspective of ZX-Calculus. I will implement it after reflecting the feedback of the last offline meeting.

@masa10-f
Copy link
Collaborator Author

masa10-f commented Aug 17, 2024

I encounterd 695 errors when I try to introduce the recommended ruff setting. I will improve my code step by step......

@masa10-f
Copy link
Collaborator Author

masa10-f commented Aug 17, 2024

I will merge this PR once after the updated TODOs are completed, which means the fundamental modules will have been implemented sparsely and properly aligned with the underlying theory.
After that we will develop a density matrix backend with a little update, self-developed ZX backend, and more advanced resource state optimizer for our research purposes

@masa10-f
Copy link
Collaborator Author

cc. @nabe98 @d1ssk

nabe98 and others added 29 commits December 16, 2024 19:01
* ✅ Add tests for remove_physical_node

* ✨ Add remove_physical_node to GraphState

* 🎨 Fix docs

* 🎨 Fix measurement_action

* 🎨 Fix measurement_action in local_complement

* ✅ Add tests for remove_clifford with restricted cases

* 🚧 Add remove_clifford only valid for restricted cases

* ✅ Add tests for Clifford removal with 4 new cases

* ✅ Add tests for exception in Clifford removal

* ✅ Add tests for exception in Clifford removal

* ✨ Update Clifford removal

* 🎨 Modified after mypy & pyright

* ♻️ Refactor remove_clifford

* 🎨 Improve test_remove_clifford_fails_for_special_clifford_vertex

* 🚧 Add necessary test case

* ✨ Add new case for Clifford removal

* 📝 Improve docs

* 💚 Enable CI on all PRs

* ♻️ Refactor test_remove_clifford

* ♻️ Refactor tests for clifford removal

* 🎨 Improve readability

* ♻️ Refactor initial graph preparation for tests

* 🎨 Modify to pass mypy & pyright

* 🐛 Fix set_output

* 🐛 Fix set_meas_angle and set_meas_plane

* 🎨 Apply is_clifford_angle

* ♻️ Refactor test_graphstate.py

* 🐛 Fix local_complement and pivot on output nodes

* 🐛 Fix bug caused by incorrect output node handling

* 🎨 Fix to pass mypy & pyright

* 🐛 Fix test_graphstate.py

* ✨ Add remove_cliffords

* 🐛 Fix remove_cliffords

* 🎨 Improve readability of remove_cliffords

* 📝 Add docstrings to _step*_action

* ⚡ Improve performance

* 🐛 Fix bug in pivot_1

* 🙈 Ignore .DS_Store

* 🚚 Move ZXGraphState into zxgraphstate.py

* 🚚 Move ZXGraphState into zxgraphstate.py

* 🎨 Rename _is_removable_clifford into _needs_nop

* 🐛 Fix bug in removing cliffords from a random graph

* ⚡ Fix condition check in remove_cliffords

* 🐛 Fix measurement actions

* 🙈 Fix .gitignore

* 🚚 Move zxgraphstate tests from graphstate tests

* ♻️ Refactor to reflect meas_bases

* 🎨 Apply ruff

---------

Co-authored-by: nabe98 <watanabeyuki@watanabeyuumarenoMacBook-Pro.local>
* 🐛 Remove side effect in focus_gflow func

* 💚 Enable CI on every PR

---------

Co-authored-by: nabe98 <watanabeyuki@watanabeyuumarenoMacBook-Pro.local>
* ✅ Add tests for convert_to_phase_gadget

* ✨ Add convert_to_phase_gadget to ZXGraphState

* 🎨 Add diagramatic representation for each tests in convert_to_phase_gadget

* ✅ Add tests for merge_yz_to_xy

* 🎨 Add merge_yz_to_xy

* 🎨 Improve tests' readability

* ✅ Add tests for merge_yz_nodes

* 🎨 Add merge_yz_nodes

* ♻️ Refactor test codes

* ✅ Add tests for prune_non_clifford

* ✨ Add prune_non_clifford to ZXGraphState

* 🎨 Apply ruff check

* ✏️ Fix typo

* 🐛 Fix merge_yz_to_xy & merge_yz_nodes

* ✨ Add get_random_gflow_circ

* ✨ Add an example for Clifford/non-Clifford removal

* 🐛 Fix _angle_check

* 🐛 Fix test_local_complement in test_euler.py

* 🐛 Fix update rule

* 🐛 Add property method

* 🎨 Apply ruff

* ✅ Add pivot tests for euler.py

* ✨ Add measurement_actions in test_zxgraphstate

* ✨ Add plane_combinations in test_zxgraphstate

* ✨ Add rng in test_zxgraphstate

* ✨ Add doc in plane_combinations

* 🎨 Change typehints

* 🐛 Fix bug in _test

* 🎨 Fix test_local_complement_with_no_edge

* 🎨 Fix test_local_complement_on_output_node

* 🎨 Fix test_local_complement_with_two_nodes_graph

* 🎨 Fix test_local_complement_with_minimal_graph

* 🎨 Fix test_local_complement_4_times

* 🚧 Migrate local_complement & pivot into new version

* 🎨 Fix lc & import

* 🔥 Remove old local_complement, pivot, remove_clifford

* 🎨 Fix typehints in test

* ♻️ Refactor test_zxgraphstate

* 🔥 Delete unneccesary tests

* 🎨 Fix measurement action readability

* 🎨 Rename get_random_gflow_circ into random_circ

* 🎨 Fix typehints for random_circ

* 🔥 Remove unnecessary del sentences

* 🎨 Rename prune_non_cliffords method into full_reduce

* 🎨 Format docstring

* ♻️ Merge if block into while block

* ♻️ Refactor check conditions

* 🎨 Migrate from get_random_gflow_circ to random_circ

* 📝 Fix docstrings

* 🐛 Fix typehint in random_objects

* 🎨 Fix typehints

* 🎨 Rewrite angle condition with _is_close_angle

* 🎨 Improve readability

* ✅ Add corner case for test_remove_clifford

* 🎨 Apply ruff

* ♻️ Refactor test_zxgraphstate

* ⚡ Improve performance for merge_yz_nodes

* 📝 Fix docstrings

* 🎨 Integrate the term 'vertex' into 'node'

* 🔥 Remove _update_new_measurement method

* 🎨 Simplify

* 🎨 Apply ruff

* 🎨 Improve docstrings and variable name

* 🐛 Fix bug in _remove_clifford

* ♻️ Refactor merge_yz_nodes

* ♻️ Refactor _extract_yz_adjacent_pair

* 🔥 Delete an uneccesary line

* ♻️ Refactor ZXGraphState

* ♻️ Refactor ZXGraphState

* 🐛 Fix bug in merge_yz_to_xy

* 🐛 Fix bug when referencing _clifford_rules

* 🚚 Rename

---------

Co-authored-by: nabe98 <watanabeyuki@watanabeyuumarenoMacBook-Pro.local>
@masa10-f masa10-f closed this Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants