Skip to content

Commit

Permalink
feat: add "tket2.qsystem.random" extension (#779)
Browse files Browse the repository at this point in the history
Closes: #748

Apart from the signatures in the reporting issue, I had to include an
additional function to discard the linear context.
```rust
    /// fn delete_rng_context(RNGContext) -> ()
    DeleteContext,
```

Appreciate other ideas to discard the context.
  • Loading branch information
qartik authored Feb 20, 2025
1 parent 175a02d commit f4901ee
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tket2-exts/src/tket2_exts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def qsystem() -> Extension:
return load_extension("tket2.qsystem")


@functools.cache
def qsystem_random() -> Extension:
return load_extension("tket2.qsystem.random")


@functools.cache
def qsystem_utils() -> Extension:
return load_extension("tket2.qsystem.utils")
Expand Down
354 changes: 354 additions & 0 deletions tket2-exts/src/tket2_exts/data/tket2/qsystem/random.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
{
"version": "0.1.0",
"name": "tket2.qsystem.random",
"runtime_reqs": [
"prelude"
],
"types": {
"context": {
"extension": "tket2.qsystem.random",
"name": "context",
"params": [
{
"tp": "Type",
"b": "A"
}
],
"description": "The linear RNG context type",
"bound": {
"b": "Explicit",
"bound": "A"
}
}
},
"values": {},
"operations": {
"DeleteRNGContext": {
"extension": "tket2.qsystem.random",
"name": "DeleteRNGContext",
"description": "Discard the given RNG context.",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
}
],
"output": [],
"runtime_reqs": []
}
},
"binary": false
},
"NewRNGContext": {
"extension": "tket2.qsystem.random",
"name": "NewRNGContext",
"description": "Seed the RNG and return a new RNG context. Required before using other RNG ops, can be called only once.",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
],
"output": [
{
"t": "Sum",
"s": "General",
"rows": [
[],
[
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
}
]
]
}
],
"runtime_reqs": []
}
},
"binary": false
},
"RandomFloat": {
"extension": "tket2.qsystem.random",
"name": "RandomFloat",
"description": "Generate a random floating point value in the range [0,1).",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
},
{
"t": "Opaque",
"extension": "arithmetic.float.types",
"id": "float64",
"args": [],
"bound": "C"
}
],
"runtime_reqs": []
}
},
"binary": false
},
"RandomInt": {
"extension": "tket2.qsystem.random",
"name": "RandomInt",
"description": "Generate a random 32-bit unsigned integer.",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
},
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 5
}
],
"bound": "C"
}
],
"runtime_reqs": []
}
},
"binary": false
},
"RandomIntBounded": {
"extension": "tket2.qsystem.random",
"name": "RandomIntBounded",
"description": "Generate a random 32-bit unsigned integer less than `bound`.",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
},
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 5
}
],
"bound": "C"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.qsystem.random",
"id": "context",
"args": [
{
"tya": "Type",
"ty": {
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 6
}
],
"bound": "C"
}
}
],
"bound": "A"
},
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "BoundedNat",
"n": 5
}
],
"bound": "C"
}
],
"runtime_reqs": []
}
},
"binary": false
}
}
}
3 changes: 2 additions & 1 deletion tket2-hseries/src/bin/tket2-hseries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ fn main() {
tket2::extension::rotation::ROTATION_EXTENSION.to_owned(),
tket2_hseries::extension::qsystem::EXTENSION.to_owned(),
tket2_hseries::extension::futures::EXTENSION.to_owned(),
tket2_hseries::extension::random::EXTENSION.to_owned(),
tket2_hseries::extension::result::EXTENSION.to_owned(),
tket2_hseries::extension::wasm::EXTENSION.to_owned(),
tket2_hseries::extension::utils::EXTENSION.to_owned(),
tket2_hseries::extension::wasm::EXTENSION.to_owned(),
]);

args.run_dump(&reg);
Expand Down
1 change: 1 addition & 0 deletions tket2-hseries/src/extension.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This module defines the Hugr extensions used by tket2-hseries.
pub mod futures;
pub mod qsystem;
pub mod random;
pub mod result;
pub mod utils;
pub mod wasm;
Loading

0 comments on commit f4901ee

Please sign in to comment.