Skip to content

Commit d6ffb62

Browse files
authored
Merge pull request #34 from loadnetwork/roqoqo
Roqoqo
2 parents afb5bbd + 2513863 commit d6ffb62

File tree

4 files changed

+122
-10
lines changed

4 files changed

+122
-10
lines changed

native/quantum_runtime_nif/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Load Network
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## About
2+
3+
The `quantum_runtime_nif` is the foundation of the `~quantum-rt@1.0` device: a quantum computing runtime built on top of roqoqo simulation framework. This hyperbeam device enables serverless quantum function execution, positioning hyperbeam nodes running this device as providers of serverless functions compute.
4+
5+
The device supports quantum circuit execution, measurement-based quantum computation, and provides a registry of pre-built quantum functions including superposition states, quantum random number generation, and quantum teleportation protocols.
6+
7+
***[!] This device is currently simulation-based using roqoqo-quest backend - for educational purposes only [!]***
8+
9+
### What is Quantum Computing?
10+
11+
Quantum computing make use of quantum mechanical phenomena such as superposition and entanglement to process information in fundamentally different ways than classical computers.
12+
13+
Unlike classical bits that exist in definite states (0 or 1), quantum bits (qubits) can exist in superposition of both states simultaneously, enabling parallel computation across multiple possibilities.
14+
15+
## ~quantum-rt@1.0 device
16+
17+
The `~quantum-rt@1.0` device, as per its current implementation, provides a serverless quantum function execution environment. It uses the roqoqo simulation backend for development and testing, but can be adapted to real quantum computation using services like [AQT.eu](https://aqt.eu) or other quantum cloud providers such as IBM Quantum Platform, with minimal device code changes.
18+
19+
The device supports quantum circuits with up to 32 qubits and provides a registry of whitelisted quantum functions that can be executed through HTTP calls or via ao messaging.
20+
21+
### Available Quantum Functions (in simulation mode)
22+
23+
- **superposition**: creates quantum superposition state on a single qubit
24+
- **quantum_rng**: quantum (pseuo)random number generator using multiple qubits
25+
- **bell_state**: creates entangled Bell states between qubits
26+
- **quantum_teleportation**: implements quantum teleportation protocol
27+
28+
## Quantum Runtime Technical Architecture
29+
30+
```rust
31+
#[rustler::nif]
32+
fn hello() -> NifResult<String> {
33+
Ok("Hello world!".to_string())
34+
}
35+
36+
#[rustler::nif(schedule = "DirtyCpu")]
37+
fn compute(
38+
num_qubits: usize,
39+
function_id: String,
40+
measurements: Vec<usize>,
41+
) -> NifResult<HashMap<String, f64>> {
42+
let runtime = Runtime::new(num_qubits);
43+
match runtime.execute_serverless(function_id, measurements) {
44+
Ok(result) => Ok(result),
45+
Err(_) => Err(rustler::Error::Term(Box::new("execution failed"))),
46+
}
47+
}
48+
```
49+
50+
The compute() function takes 3 inputs: the number of qubits to initialize, a function ID from the serverless registry, and a list of qubit indices to measure. It returns a HashMap containing the measurement results.
51+
52+
```mermaid
53+
graph TD
54+
Qubits[Number of Qubits] --> Runtime[Runtime::new]
55+
FuncID[Function ID] --> Registry[Function Registry]
56+
Measurements[Measurement Indices] --> Runtime
57+
58+
Runtime --> Backend[roqoqo-quest Backend]
59+
Registry --> Circuit[Quantum Circuit]
60+
Circuit --> Backend
61+
62+
Backend --> Simulate[Quantum Simulation]
63+
Simulate --> Measure[Measure Qubits]
64+
Measure --> Results[HashMap Results]
65+
66+
Results --> NIF[Return to dev_quantum.erl]
67+
68+
style Backend fill:#e1f5fe
69+
style Simulate fill:#f3e5f5
70+
style Results fill:#e8f5e8
71+
```
72+
73+
## Device API Examples
74+
75+
#### Generate Quantum Random Numbers
76+
77+
```bash
78+
curl -X POST "https://hb.load.rs/~quantum-rt@1.0/compute" \
79+
-H "Content-Type: application/json" \
80+
-d '{
81+
"function_id": "quantum_rng",
82+
"num_qubits": 4,
83+
"measurements": [0, 1, 2, 3]
84+
}'
85+
```
86+
87+
## References
88+
89+
- hb device interface: [dev_quantum.erl](../../src/dev_quantum.erl)
90+
- nif interface: [quantum_runtime_nif.erl](../../src/quantum_runtime_nif.erl)
91+
- quantum functions registry: [registry.rs](./src/core/registry.rs)
92+
- runtime core: [runtime.rs](./src/core/runtime.rs)
93+
94+
## License
95+
This device is licensed under the [MIT License](./LICENSE)

native/quantum_runtime_nif/src/core/registry.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::core::runtime::Runtime;
2+
use crate::tests::functions::{quantum_rng, superposition};
23
use roqoqo::Circuit;
34
use std::collections::HashMap;
4-
use crate::tests::functions::{superposition, quantum_rng};
55

66
impl Runtime {
77
pub fn get_function_registry(&self) -> HashMap<String, Box<dyn Fn() -> Circuit>> {
@@ -10,16 +10,12 @@ impl Runtime {
1010

1111
registry.insert(
1212
"superposition".to_string(),
13-
Box::new(move || {
14-
superposition(1)
15-
}),
13+
Box::new(move || superposition(1)),
1614
);
1715

1816
registry.insert(
1917
"quantum_rng".to_string(),
20-
Box::new(move || {
21-
quantum_rng(max_qubits)
22-
}),
18+
Box::new(move || quantum_rng(max_qubits)),
2319
);
2420

2521
registry

native/quantum_runtime_nif/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pub mod core;
22
pub mod tests;
3+
use crate::core::runtime::Runtime;
34
use rustler::NifResult;
45
use std::collections::HashMap;
5-
use crate::core::runtime::Runtime;
66

77
#[rustler::nif]
88
fn hello() -> NifResult<String> {
@@ -16,11 +16,11 @@ fn compute(
1616
measurements: Vec<usize>,
1717
) -> NifResult<HashMap<String, f64>> {
1818
let runtime = Runtime::new(num_qubits);
19-
19+
2020
match runtime.execute_serverless(function_id, measurements) {
2121
Ok(result) => Ok(result),
2222
Err(_) => Err(rustler::Error::Term(Box::new("execution failed"))),
2323
}
2424
}
2525

26-
rustler::init!("quantum_runtime_nif", [hello, compute]);
26+
rustler::init!("quantum_runtime_nif", [hello, compute]);

0 commit comments

Comments
 (0)