Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SiblingSubgraph generates a HUGR with two output nodes #1943

Open
CalMacCQ opened this issue Feb 21, 2025 · 1 comment
Open

SiblingSubgraph generates a HUGR with two output nodes #1943

CalMacCQ opened this issue Feb 21, 2025 · 1 comment

Comments

@CalMacCQ
Copy link

CalMacCQ commented Feb 21, 2025

I've done a simple BFS to construct a subgraph from an input circuit and a starting node.

the input circuit HUGR looks like this. Load in this json file -> goto.json

Image

After extracting the subgraph the HUGR we see that it has multiple output nodes which seems strange. See the extra ouput node hanging by itself on the right of the diagram below.

Image

Sorry I don't know of a short way to reproduce this issue so I'll just share the code I hacked together.

use tket2::{
    self,
    hugr::{hugr::views::SiblingSubgraph, ops::NamedOp, HugrView, Node},
};

fn subgraph_from_source_node(circuit: &tket2::Circuit, source_node: Node) -> SiblingSubgraph {
    // Traverse the Circuit with BFS to find the subcircuit
    // in the causal cone of the source node

    let mut visited_nodes: Vec<Node> = Vec::new();
    let mut nodes_to_visit = vec![source_node];

    visited_nodes.push(source_node);

    while nodes_to_visit.len() > 0 {
        let current_node = nodes_to_visit.pop().unwrap();
        for neighbour in circuit.hugr().output_neighbours(current_node) {
            if !visited_nodes.contains(&neighbour) {
                visited_nodes.push(neighbour);
                nodes_to_visit.push(neighbour);
            }
        }
    }
    SiblingSubgraph::try_from_nodes(visited_nodes, circuit.hugr()).unwrap()
}

fn main() {
    let circ: tket2::Circuit = tket2::serialize::load_tk1_json_file("goto.json").unwrap();

    let pauli_z_cmds: Vec<_> = circ
        .commands()
        .filter(|cmd| cmd.optype().name() == "tket2.quantum.Z")
        .collect();

    let first_pauli = &pauli_z_cmds[0];

    let sibgraph = subgraph_from_source_node(&circ, first_pauli.node());

    let extracted = sibgraph.extract_subgraph(circ.hugr(), "");

    let subcircuit = tket2::Circuit::try_new(&extracted, extracted.root()).unwrap();

    println!("{}", subcircuit.mermaid_string());
}
@CalMacCQ CalMacCQ changed the title SiblingSubgraph generates a HUGR with multiple output nodes SiblingSubgraph generates a HUGR with two output nodes Feb 21, 2025
@CalMacCQ
Copy link
Author

CalMacCQ commented Mar 1, 2025

More of a HUGR issue rather than tket2. I think it should be moved to that repository.

@CalMacCQ CalMacCQ transferred this issue from CQCL/tket2 Mar 3, 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

No branches or pull requests

1 participant