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

Add samples of testing Q# code that prepares a quantum state #1873

Merged
merged 3 commits into from
Aug 23, 2024

Conversation

tcNickolas
Copy link
Member

Closes #1268.

Copy link

Benchmark for c109a31

Click to view benchmark
Test Base PR %
Array append evaluation 339.8±8.24µs 339.7±2.73µs -0.03%
Array literal evaluation 168.8±1.06µs 170.2±5.25µs +0.83%
Array update evaluation 420.1±9.33µs 417.2±3.63µs -0.69%
Core + Standard library compilation 24.3±1.11ms 25.3±1.03ms +4.12%
Deutsch-Jozsa evaluation 5.0±0.11ms 5.0±0.08ms 0.00%
Large file parity evaluation 34.8±0.17ms 34.9±0.27ms +0.29%
Large input file compilation 14.7±0.44ms 15.3±0.81ms +4.08%
Large input file compilation (interpreter) 57.2±2.08ms 58.6±1.97ms +2.45%
Large nested iteration 32.9±0.31ms 32.9±0.29ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1597.1±113.55µs 1620.3±176.08µs +1.45%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.1±0.16ms 8.2±0.22ms +1.23%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1465.2±104.94µs 1463.6±93.97µs -0.11%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 28.1±0.35ms 28.4±0.40ms +1.07%
Teleport evaluation 93.6±3.72µs 94.3±4.09µs +0.75%

samples/testing/states/README.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@swernli swernli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple minor comments, but nothing that should prevent merging. Up to your discretion on those!

@tcNickolas tcNickolas enabled auto-merge August 23, 2024 19:37
Comment on lines +18 to +40
qsharp.eval(f"use qs = Qubit[2]; StatePrep.PrepareStateWithComplexPhases(qs);")
# Get the state of the allocated qubits and convert it to a dense vector.
state = qsharp.dump_machine().as_dense_state()
# Compare two vectors.
assert state == pytest.approx(expected_state)


def test_state_exact_rejects_global_phase() -> None:
"""Test that shows that the exact check from the previous test fails if the state is different by a global phase."""
# Run Q# code that allocates the qubits and prepares the state but doesn't deallocate the qubits.
qsharp.eval(f"use qs = Qubit[2]; StatePrep.PrepareStateWithGlobalPhase(qs);")
# Get the state of the allocated qubits and convert it to a dense vector.
state = qsharp.dump_machine().as_dense_state()
# Compare two vectors. Here we expect them to be _not equal_ due to the global phase -1.
assert state != pytest.approx(expected_state)


def test_state_global_phase() -> None:
"""Test that Q# code prepares the expected state up to a global phase using Python test code."""
# Run Q# code that allocates the qubits and prepares the state but doesn't deallocate the qubits.
qsharp.eval(f"use qs = Qubit[2]; StatePrep.PrepareStateWithGlobalPhase(qs);")
# Get the state of the allocated qubits.
state = qsharp.dump_machine()

Check notice

Code scanning / devskim

If untrusted data (data from HTTP requests, user submitted files, etc.) is included in an eval statement it can allow an attacker to inject their own code. Note test

Review eval for untrusted data
@tcNickolas tcNickolas added this pull request to the merge queue Aug 23, 2024
Merged via the queue into main with commit 57f7f6e Aug 23, 2024
18 checks passed
@tcNickolas tcNickolas deleted the mariia/test_dump_machine branch August 23, 2024 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add samples of testing Q# code
3 participants