Skip to content

Comments

🐛 Remove side effect in focus_gflow func#9

Merged
nabe98 merged 2 commits intomffrom
fix-focus_gflow
Dec 17, 2024
Merged

🐛 Remove side effect in focus_gflow func#9
nabe98 merged 2 commits intomffrom
fix-focus_gflow

Conversation

@nabe98
Copy link
Collaborator

@nabe98 nabe98 commented Dec 17, 2024

  • Fix bug in focus_gflow
  • Enable CI on every PR

@nabe98 nabe98 self-assigned this Dec 17, 2024
@nabe98 nabe98 requested a review from masa10-f December 17, 2024 06:38
@masa10-f
Copy link
Collaborator

@nabe98 Did you check this change fixes the side effect on your program?

@nabe98
Copy link
Collaborator Author

nabe98 commented Dec 17, 2024

@masa10-f
Yes. I used the following codes to check the side effect.

# %%
import numpy as np

from graphix_zx.circuit import MBQCCircuit, circuit2graph
from graphix_zx.zxgraphstate import ZXGraphState
from graphix_zx.focus_flow import focus_gflow, is_focused


def random_gflow_circ(
    width: int,
    depth: int,
    rng: np.random.Generator = np.random.default_rng(),
    edge_p: float = 0.5,
) -> MBQCCircuit:
    """Generate a random MBQC circuit which has gflow

    Parameters
    ----------
    width : int
        circuit width
    depth : int
        circuit depth
    rng : np.random.Generator, optional
        random number generator, by default np.random.default_rng()
    edge_p : float, optional
        probability of adding CZ gate, by default 0.5

    Returns
    -------
    MBQCCircuit
        generated MBQC circuit
    """
    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

circ = random_gflow_circ(4, 4)
graph, flow = circuit2graph(circ)
print(flow)

zxgraph = ZXGraphState()
zxgraph.append(graph)
flow_opt = focus_gflow(flow, zxgraph)
print(flow)

print(is_focused(flow, graph))
print(is_focused(flow_opt, graph))
print("flow == flow_opt: ", flow == flow_opt)
# %%

In the previous implementation, flow == flow_opt evaluated to True, but with this modification, it now returns False. Additionally, I confirmed that flow remains unaffected (in two print(flow) lines).

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.

Sure! You can merge!

@nabe98 nabe98 merged commit aaae09a into mf Dec 17, 2024
@nabe98 nabe98 deleted the fix-focus_gflow branch December 17, 2024 07:22
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.

2 participants