Skip to content

When reading from model formats, read_envelope() doesn't add extra extensions (encoded in the envelope) to the Package #2589

@cqc-alec

Description

@cqc-alec

Save the following serialized hugr to a file named pkg_with_ext_model_ext:

HUGRiHJv)@[{"version":"0.1.0","name":"miniquantum","types":{},"operations":{"CX":{"extension":"miniquantum","name":"CX","description":"CNOT gate","signature":{"params":[],"body":{"input":[{"t":"Q"},{"t":"Q"}],"output":[{"t":"Q"},{"t":"Q"}]}},"binary":false},"H":{"extension":"miniquantum","name":"H","description":"Hadamard gate","signature":{"params":[],"body":{"input":[{"t":"Q"}],"output":[{"t":"Q"}]}},"binary":false}}}](hugr 0)

(mod)

(import core.entrypoint)

(import core.title)

(import prelude.qubit)

(import core.fn)

(import core.meta.description)

(declare-operation miniquantum.H (core.fn [prelude.qubit] [prelude.qubit])
  (meta (core.meta.description "Hadamard gate")))

(define-func private _1 (core.fn [prelude.qubit] [prelude.qubit])
  (meta (core.title "main"))
  (dfg [%0] [%1]
    (signature (core.fn [prelude.qubit] [prelude.qubit]))
    (dfg [%0] [%1]
      (signature (core.fn [prelude.qubit] [prelude.qubit]))
      (meta core.entrypoint)
      (dfg [%2] [%3]
        (signature (core.fn [prelude.qubit] [prelude.qubit]))
        (miniquantum.H [%2] [%3]
          (signature (core.fn [prelude.qubit] [prelude.qubit])))))))

Run this code:

use hugr::envelope::{write_envelope, EnvelopeConfig};
use hugr::{
    envelope::read_envelope, std_extensions::std_reg
};
use std::fs::File;
use std::io::{BufReader, Cursor};

fn main() {
    let f = File::open("pkg_with_ext_model_text").unwrap();
    let buff = BufReader::new(f);
    let (_, pkg) = read_envelope(buff, &std_reg()).unwrap();
    println!("{}", pkg.extensions);
    let mut output: Vec<u8> = Vec::new();
    let _ = write_envelope(&mut output, &pkg, EnvelopeConfig::binary());
    let buff1 = Cursor::new(output);
    let (_, pkg1) = read_envelope(buff1, &std_reg()).unwrap();
    println!("{}", pkg1.extensions);
}

Output:

ExtensionRegistry[]

thread 'main' panicked at src/main.rs:16:54:
called `Result::unwrap()` on an `Err` value: ModelImport { source: ImportError { inner: ExtensionResolution(MissingOpExtension { node: Some(Node(7)), op: "OpaqueOp:miniquantum.H", missing_extension: IdentList("miniquantum"), available_extensions: [IdentList("arithmetic.conversions"), IdentList("arithmetic.float"), IdentList("arithmetic.float.types"), IdentList("arithmetic.int"), IdentList("arithmetic.int.types"), IdentList("collections.array"), IdentList("collections.borrow_arr"), IdentList("collections.list"), IdentList("collections.static_array"), IdentList("collections.value_array"), IdentList("logic"), IdentList("prelude"), IdentList("ptr")] }), generator: None } }

We can see that the package imported by read_envelope() has an empty ExtensionRegistry, whereas this should have been populated with the extension encoded in the envelope.

When we export this package to a binary envelope and attempt to re-import it we get an error.

This affects, for example, any serialized hugr that uses the tket extension.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugfixCategory: PR with bug fix

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions