Skip to content

Comments

Add Clifford Removal Function#7

Merged
nabe98 merged 50 commits intomffrom
remove-clifford-vertices
Dec 16, 2024
Merged

Add Clifford Removal Function#7
nabe98 merged 50 commits intomffrom
remove-clifford-vertices

Conversation

@nabe98
Copy link
Collaborator

@nabe98 nabe98 commented Oct 21, 2024

Enables Clifford vertex removal.

  • For [1], cases corresponding to the following lemmas are implemented

    • Lemma 4.7
    • Lemma 4.8
    • Lemma 4.9
    • Lemma 4.11
    • Lemma 4.12
  • Tests

    • Lemma 4.7
    • Lemma 4.8
    • Lemma 4.9
    • Lemma 4.11
    • Lemma 4.12

[1] https://arxiv.org/abs/2003.01664

closes: #6

@masa10-f
Copy link
Collaborator

@nabe98
Could you check your code with mypy and pyright before my review? (although I know it's under development)

@nabe98 nabe98 self-assigned this Oct 24, 2024
Copy link
Collaborator

@masa10-f masa10-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! I added a few comments, so please refer to them.

Also, could you move ZXGraphState class into a new file, like zxgraphstate.py?

@masa10-f
Copy link
Collaborator

masa10-f commented Nov 8, 2024

@nabe98 Could you also turn on the format/type check ci?

@nabe98 nabe98 requested a review from masa10-f November 8, 2024 13:07
@masato-fuk
Copy link
Contributor

masato-fuk commented Nov 13, 2024

@nabe98

When I run the following code, after Cliffords are removed from the original graph, there seems no gflow. Do you have any idea about this?

# %%
import numpy as np
import networkx as nx
import fastflow as ff
from fastflow import gflow
from graphix_zx.common import Plane
from graphix_zx.euler import is_clifford_angle
from graphix_zx.flow import FlowLike
from graphix_zx.graphstate import BaseGraphState
from graphix_zx.zxgraphstate import ZXGraphState
from graphix_zx.circuit import MBQCCircuit, circuit2graph
from graphix_zx.visualizer import visualize


def random_gflow_circ(
    width: int,
    depth: int,
    rng: np.random.Generator = np.random.default_rng(),
    edge_p: float = 0.5,
) -> MBQCCircuit:
    angle_list = [0, np.pi / 3, 2 * np.pi / 3, np.pi]
    circ = MBQCCircuit(width)
    for d in range(depth):
        for j in range(width):
            circ.j(j, rng.choice(angle_list))
        if d < depth - 1:
            for j in range(width):
                if rng.random() < edge_p:
                    circ.cz(j, (j + 1) % width)
            num = rng.integers(0, width)
            if num > 0:
                target = set(rng.choice([i for i in range(width)], num))
                circ.phase_gadget(target, rng.choice(angle_list))

    return circ


def check_clifford_removal(graphstate: BaseGraphState) -> bool:
    no_clifford = True
    for node in (
        graphstate.physical_nodes - graphstate.output_nodes - graphstate.input_nodes
    ):
        angle = graphstate.meas_angles[node]
        no_clifford &= not is_clifford_angle(angle)
    return no_clifford


def gflow_wrapper(graphstate: BaseGraphState) -> FlowLike:
    graph = nx.Graph()
    graph.add_nodes_from(graphstate.physical_nodes)
    graph.add_edges_from(graphstate.physical_edges)

    planes = graphstate.meas_planes
    fastflow_planes = {}
    for node, plane in planes.items():
        if plane == Plane.XY:
            fastflow_planes[node] = ff.common.Plane.XY
        elif plane == Plane.YZ:
            fastflow_planes[node] = ff.common.Plane.YZ
        elif plane == Plane.XZ:
            fastflow_planes[node] = ff.common.Plane.XZ
        else:
            msg = f"No match {plane}"
            raise ValueError(msg)

    gflow_object = gflow.find(
        graph, graphstate.input_nodes, graphstate.output_nodes, fastflow_planes
    ).f

    return gflow_object


# %%

# generate circuit

# graph, flow = get_random_flow_graph(5, 5)
circ = random_gflow_circ(5, 5)
graph, flow = circuit2graph(circ)
# visualize(graph, save=True, filename="test.png")
gflow_result = gflow_wrapper(graph)
print(gflow_result)

# %%

# ZX process

zxgraph = ZXGraphState()
zxgraph.append(graph)

# %%
zxgraph.remove_cliffords()
print("No Clifford?", check_clifford_removal(zxgraph))
# visualize(zxgraph, save=True, filename="zxtest.png")
gflow_result = gflow_wrapper(zxgraph)
print(gflow_result)

@nabe98
Copy link
Collaborator Author

nabe98 commented Dec 2, 2024

@masa10-f
Sorry for the delayed response! The issue was caused by some errors in the measurement actions. I have confirmed that the gflow is successfully saved when running the sample code above.

Since it relies on the fastflow module, I didn't include the test to confirm that it preserves gflow. Do you think it should be included?

Copy link
Collaborator

@masa10-f masa10-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nabe98 Hi! I added a few comments on the code. Please check them.

@masa10-f
Copy link
Collaborator

@nabe98 Could you move tests for ZXGraphState into a new file because the original file becomes too big to maintain?

@nabe98 nabe98 requested a review from masa10-f December 13, 2024 07:42
@masa10-f
Copy link
Collaborator

@nabe98 Could you check ruff? Afterwards, you can merge this branch! Well done!
I'd like to simply the tests for maintainability, but it might be addressed in another issue.

@nabe98
Copy link
Collaborator Author

nabe98 commented Dec 16, 2024

@masa10-f Thanks for reviewing!
I completed the ruff check now.

@nabe98 nabe98 merged commit 3a62a69 into mf Dec 16, 2024
@nabe98 nabe98 deleted the remove-clifford-vertices branch December 16, 2024 10:01
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.

3 participants