-
Notifications
You must be signed in to change notification settings - Fork 2
feat(ppvm-cli): add the ppvm command-line binary #175
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
Open
david-pl
wants to merge
3
commits into
david/42.3-composite
Choose a base branch
from
david/42.4-cli
base: david/42.3-composite
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "ppvm-cli" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| clap = { version = "4.6.1", features = ["derive"] } | ||
| eyre = "0.6.12" | ||
| ppvm-vihaco = { version = "0.1.0", path = "../ppvm-vihaco", features = ["rayon"] } | ||
|
|
||
| [[bin]] | ||
| name = "ppvm" | ||
| path = "src/main.rs" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # ppvm-cli | ||
|
|
||
| Command-line front-end for the Pauli-propagation virtual machine. Parses, | ||
| dumps, runs, and steps through `.sst` programs (and their compiled `.ssb` | ||
| bytecode). | ||
|
|
||
| ## Install | ||
|
|
||
| From this crate directory: | ||
|
|
||
| ```sh | ||
| cargo install --path crates/ppvm-cli | ||
| ``` | ||
|
|
||
| This builds the release binary and copies it to `~/.cargo/bin/ppvm`. As long as | ||
| `~/.cargo/bin` is on your `PATH`, you can then invoke `ppvm` from anywhere. | ||
|
|
||
| During development you can skip the install and use `cargo run` instead — just | ||
| put CLI arguments after `--`: | ||
|
|
||
| ```sh | ||
| cargo run -p ppvm-cli -- run examples/ghz.sst | ||
| ``` | ||
|
|
||
| ## Run | ||
|
|
||
| `run` executes a program for one or more shots and prints the measurement | ||
| results. The example [`examples/ghz.sst`](examples/ghz.sst) prepares a 3-qubit | ||
| GHZ state and measures every qubit, so each shot reads `000` or `111`: | ||
|
|
||
| ```sh | ||
| $ ppvm run examples/ghz.sst | ||
| 000 | ||
| ``` | ||
|
|
||
| Each shot is printed as a single flat bit string — `0`/`1`, with a lost qubit | ||
| shown as `2` — and shots are separated by newlines. Use `-s`/`--shots` to run | ||
| more than one: | ||
|
|
||
| ```sh | ||
| $ ppvm run examples/ghz.sst --shots 5 | ||
| 000 | ||
| 000 | ||
| 111 | ||
| 000 | ||
| 111 | ||
| ``` | ||
|
|
||
| Other options: | ||
|
|
||
| - `-t`/`--threads <N>` — run shots across `N` threads. More than one enables | ||
| parallel execution (defaults to 1). | ||
| - `--seed <N>` — seed the RNG for reproducible results. The same seed yields the | ||
| same shots regardless of the thread count. | ||
| - `-o`/`--output <FILE>` — write the results to a file (one shot per line) | ||
| instead of stdout. | ||
| - `-f debug` — print the raw record for every shot instead of bit strings. | ||
| - `-q`/`--quiet` — run without printing anything. | ||
|
|
||
| ```sh | ||
| $ ppvm run examples/ghz.sst --shots 2 -f debug | ||
| [[[Zero], [Zero], [Zero]], [[One], [One], [One]]] | ||
|
|
||
| $ ppvm run examples/ghz.sst --shots 1000 --threads 8 -o results.txt | ||
| Results written to results.txt | ||
| ``` | ||
|
|
||
| ## Dump | ||
|
|
||
| `dump` compiles a `.sst` program to `.ssb` bytecode. With no `-o`, it writes | ||
| next to the input (`ghz.sst` → `ghz.ssb`): | ||
|
|
||
| ```sh | ||
| $ ppvm dump examples/ghz.sst | ||
| Bytecode written to examples/ghz.ssb | ||
| ``` | ||
|
|
||
| `run` auto-detects the format from the file's contents, so the bytecode runs the | ||
| same way as the source: | ||
|
|
||
| ```sh | ||
| $ ppvm run examples/ghz.ssb | ||
| 000 | ||
| ``` | ||
|
|
||
| `dump` refuses to overwrite an existing file unless you pass `-f`/`--force`. | ||
|
|
||
| ## Debug | ||
|
|
||
| `debug` steps through a program interactively. At each pause it prints the | ||
| program counter, the next instruction, and the measurements so far, then waits | ||
| for a command (type the letter and press Enter; a bare Enter steps): | ||
|
|
||
| - `s` — step one instruction | ||
| - `c` — continue to the next breakpoint (or the end) | ||
| - `q` — quit | ||
|
|
||
| By default it pauses at `breakpoint` instructions in the program. Add one | ||
| wherever you want execution to stop: | ||
|
|
||
| ``` | ||
| fn @main() { | ||
| const.u64 0 | ||
| circuit.h | ||
|
|
||
| // execution pauses here | ||
| breakpoint | ||
|
|
||
| const.u64 0 | ||
| circuit.measure | ||
| ret | ||
| } | ||
| ``` | ||
|
|
||
| ```sh | ||
| $ printf 's\nc\n' | ppvm debug program.sst | ||
| -- breakpoint hit -- | ||
| pc=3 next: const.u64 0 | ||
| measurements: | ||
| > s step | c continue | q quit: pc=4 next: Measure | ||
| measurements: | ||
| > s step | c continue | q quit: Program finished. | ||
| Measurements: 0 | ||
| ``` | ||
|
|
||
| To step through a program that has no breakpoints, pass `-b`/`--break-at-start` | ||
| to pause before the very first instruction: | ||
|
|
||
| ```sh | ||
| $ ppvm debug examples/ghz.sst -b | ||
| pc=0 next: const.u64 0 | ||
| measurements: | ||
| > s step | c continue | q quit: | ||
| ``` | ||
|
|
||
| Batch `run` ignores `breakpoint` instructions entirely, so the same file still | ||
| runs straight through with `ppvm run`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| device circuit.n_qubits 5; | ||
|
|
||
| // Data: q0, q1, q2. | ||
| // Syndrome ancillas: q3, q4. | ||
| fn @main() { | ||
| const.u64 1 | ||
| const.f64 0.25 | ||
| const.f64 0.0 | ||
| const.f64 0.0 | ||
| circuit.paulierror | ||
|
|
||
| const.u64 0 | ||
| const.u64 3 | ||
| circuit.cnot | ||
| const.u64 1 | ||
| const.u64 3 | ||
| circuit.cnot | ||
| const.u64 3 | ||
| circuit.measure | ||
|
|
||
| const.u64 1 | ||
| const.u64 4 | ||
| circuit.cnot | ||
| const.u64 2 | ||
| const.u64 4 | ||
| circuit.cnot | ||
| const.u64 4 | ||
| circuit.measure | ||
|
|
||
| const.u32 1 | ||
| eq.u32 | ||
| cond_br @s12_one, @s12_zero | ||
|
|
||
| @s12_one: | ||
| const.u32 1 | ||
| eq.u32 | ||
| cond_br @correct_q1, @correct_q2 | ||
|
|
||
| @s12_zero: | ||
| const.u32 1 | ||
| eq.u32 | ||
| cond_br @correct_q0, @readout | ||
|
|
||
| @correct_q0: | ||
| const.u64 0 | ||
| circuit.x | ||
| br @readout | ||
|
|
||
| @correct_q1: | ||
| const.u64 1 | ||
| circuit.x | ||
| br @readout | ||
|
|
||
| @correct_q2: | ||
| const.u64 2 | ||
| circuit.x | ||
|
|
||
| @readout: | ||
| const.u64 0 | ||
| circuit.measure | ||
| const.u64 1 | ||
| circuit.measure | ||
| const.u64 2 | ||
| circuit.measure | ||
| ret | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| device circuit.n_qubits 3; | ||
|
|
||
| // Prepare a 3-qubit GHZ state (|000> + |111>)/sqrt(2) and measure every qubit. | ||
| // The three outcomes are perfectly correlated, so each shot reads 0 0 0 or 1 1 1. | ||
| fn @main() { | ||
| const.u64 0 | ||
| circuit.h | ||
|
|
||
| const.u64 0 | ||
| const.u64 1 | ||
| circuit.cnot | ||
|
|
||
| const.u64 1 | ||
| const.u64 2 | ||
| circuit.cnot | ||
|
|
||
| const.u64 0 | ||
| circuit.measure | ||
|
|
||
| const.u64 1 | ||
| circuit.measure | ||
|
|
||
| const.u64 2 | ||
| circuit.measure | ||
|
|
||
| ret | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| device circuit.n_qubits 2; | ||
| device circuit.backend paulisum; | ||
| device circuit.observable 1.0*ZZ+0.5*XX; | ||
| device circuit.coefficient_threshold 1e-10; | ||
|
|
||
| // Heisenberg-picture run on a PauliSum target. The header observable | ||
| // `1.0*ZZ + 0.5*XX` seeds two terms; tracing `[XZ]?*` filters both ZZ | ||
| // (coef 1.0) and XX (coef 0.5), so the trace is their coefficient sum. | ||
| // | ||
| // No gates here, so the trace returns the seeded observable's coefficient | ||
| // sum directly: 1.0 + 0.5 = 1.5. Add `circuit.cnot; circuit.h; circuit.truncate` | ||
| // in textbook-reversed order to evolve before tracing. | ||
| fn @main() { | ||
| const.str "[XZ]?*" | ||
| circuit.trace | ||
| ret | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.