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

Can't define a circuit with constant parameters #1166

Closed
aborgna-q opened this issue Jun 5, 2024 · 2 comments · Fixed by #1142
Closed

Can't define a circuit with constant parameters #1166

aborgna-q opened this issue Jun 5, 2024 · 2 comments · Fixed by #1142
Labels
bug Something isn't working

Comments

@aborgna-q
Copy link
Collaborator

The following test case defines a simple circuit:

let mut builder =
    FunctionBuilder::new("f", FunctionType::new(type_row![QB], type_row![QB]).into())?;
let [q] = builder.input_wires_arr();
let angle = builder.add_load_value(ConstF64::new(0.5));
let [q] = builder.add_dataflow_op(rz_f64(), [q, angle])?.outputs_arr();

println!("{}", builder.hugr().mermaid_string());
let res = builder.finish_hugr_with_outputs([q], &FLOAT_OPS_REGISTRY);
assert_matches!(res, Ok(_));

which corresponds to

graph LR
    subgraph 0 ["(0) FuncDefn"]
        direction LR
        1["(1) Input"]
        1--"0:0<br>qubit"-->5
        2["(2) Output"]
        3["(3) const:custom:f64(0.5)"]
        3--"0:0<br>float64"-->4
        4["(4) LoadConstant"]
        4--"0:1<br>float64"-->5
        5["(5) test.quantum.RzF64"]
        5--"0:0<br>qubit"-->2
    end
Loading

However, it fails at validation time with a SrcExceedsTgtExtension error:

InvalidHUGR(
    CantInfer(
        EdgeMismatch(
            SrcExceedsTgtExtensions {
                from: Node(
                    3,
                ),
                from_extensions: ExtensionSet(
                    {
                        IdentList(
                            "arithmetic.float.types",
                        ),
                    },
                ),
                to: Node(
                    2,
                ),
                to_extensions: ExtensionSet(
                    {},
                ),
            },
        ),
    ),
)

I reduced this error from a larger hugr definition of a circuit inside a module, which failed with a MismatchedConcreteWithLocations instead.

@aborgna-q aborgna-q added the bug Something isn't working label Jun 5, 2024
@aborgna-q
Copy link
Collaborator Author

If we swap the FunctionBuilder for a DFGBuilder then the validation works.

@aborgna-q
Copy link
Collaborator Author

#1142 fixes this, if we add a .with_extension_delta(float_types::EXTENSION_ID) to the function signature definition.

I would suggest that the error we give the user (in this case, me) for a missing extension delta in the function definition is not helpful. ExtensionError doesn't tell you much:

InvalidHUGR(
    ExtensionError(
        ExtensionError {
            parent: Node(
                1,
            ),
            parent_extensions: ExtensionSet(
                {},
            ),
            child: Node(
                7,
            ),
            child_extensions: ExtensionSet(
                {
                    IdentList(
                        "arithmetic.float.types",
                    ),
                },
            ),
        },
    ),
),

@aborgna-q aborgna-q reopened this Jun 6, 2024
github-merge-queue bot pushed a commit to CQCL/tket2 that referenced this issue Jun 6, 2024
Closes #105. Closes #108.

`num_gates` used to count every node in the top-level region, giving
unexpected results on results with constants, control flow, or anything
other than simple gates.

`num_operations` now only counts `CustomOp`s, traversing containers as
needed.

I also improved the circuit unit tests, to include circuits in modules
and circuits in `FuncDefn`s (instead of `DFG`s).

Some notes:
- Part of the tests testing parametric operations is commented out until
we solve CQCL/hugr#1166.
- Although the test circuits have function names, `Circuit::name`
returns `None`. I'll address that in another PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant