-
Notifications
You must be signed in to change notification settings - Fork 97
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
Conversation
Benchmark for c109a31Click to view benchmark
|
Co-authored-by: Scott Carda <[email protected]>
There was a problem hiding this 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!
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
Closes #1268.