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

[Samples] Add samples for testing operations in Q# #1732

Merged
merged 47 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
f1bbf83
working
Manvi-Agrawal Jul 11, 2024
0786c0b
modify tests for bell state
Manvi-Agrawal Jul 11, 2024
fd689dc
update doc comments
Manvi-Agrawal Jul 11, 2024
8033c9f
Add copyright header
Manvi-Agrawal Jul 11, 2024
10c1f2c
Add README for testing operations
Manvi-Agrawal Jul 11, 2024
d68302e
Add example of using CheckOperationsAreEqual
Manvi-Agrawal Jul 11, 2024
f769a71
Minor edit
Manvi-Agrawal Jul 11, 2024
70ff160
Update README
Manvi-Agrawal Jul 11, 2024
6b56785
improve doc comment
Manvi-Agrawal Jul 11, 2024
de8ff4f
Run Q# formatter
Manvi-Agrawal Jul 11, 2024
26b7174
restore new line
Manvi-Agrawal Jul 11, 2024
ae48a1f
Run testing samples in CI
Manvi-Agrawal Jul 13, 2024
d27b39e
cleanup
Manvi-Agrawal Jul 13, 2024
678a769
whitespace fix
Manvi-Agrawal Jul 13, 2024
6c9d70c
try to fix ci
Manvi-Agrawal Jul 13, 2024
c65b04d
add test_requirements.txt
Manvi-Agrawal Jul 13, 2024
c9d9f02
Dont execute samples for testing from Q# cmd line
Manvi-Agrawal Jul 13, 2024
636df45
Move to integration tests step in CI
Manvi-Agrawal Jul 14, 2024
a5e1779
whitespace
Manvi-Agrawal Jul 14, 2024
6f9a0fd
Apply suggestions from code review
Manvi-Agrawal Jul 16, 2024
b12b0a3
PR feedback
Manvi-Agrawal Jul 17, 2024
98310be
doc comment update
Manvi-Agrawal Jul 17, 2024
f89dea3
minor edit
Manvi-Agrawal Jul 17, 2024
77c8eaa
Merge branch 'manvi/test-dump-op' of https://github.com/Manvi-Agrawal…
Manvi-Agrawal Jul 17, 2024
5b4d049
identation fix
Manvi-Agrawal Jul 21, 2024
c5fb469
Merge remote-tracking branch 'upstream/main' into manvi/test-dump-op
Manvi-Agrawal Jul 23, 2024
f23b9b7
Apply suggestions from code review
Manvi-Agrawal Jul 23, 2024
a64d6d8
remove copyright from readme
Manvi-Agrawal Jul 23, 2024
e00a479
Merge branch 'manvi/test-dump-op' of https://github.com/Manvi-Agrawal…
Manvi-Agrawal Jul 23, 2024
a7d7a46
polish readme
Manvi-Agrawal Jul 23, 2024
7b121eb
polish readme..
Manvi-Agrawal Jul 23, 2024
d500517
rename dir
Manvi-Agrawal Jul 23, 2024
3a80844
Doc comment
Manvi-Agrawal Jul 23, 2024
bc6cd10
minor edit
Manvi-Agrawal Jul 23, 2024
9ea3cd3
use lambda appropriately
Manvi-Agrawal Jul 23, 2024
8b24e20
fix merge conflict
Manvi-Agrawal Jul 23, 2024
74a4622
rename
Manvi-Agrawal Jul 23, 2024
cf50eb6
move setting profile to setup function
Manvi-Agrawal Jul 23, 2024
27800e9
remove complex dtype in tests
Manvi-Agrawal Jul 23, 2024
e69e594
minor edit
Manvi-Agrawal Jul 23, 2024
9d84a37
Q# formatter fix
Manvi-Agrawal Jul 25, 2024
dbf1504
remove target profile from tests
Manvi-Agrawal Jul 26, 2024
00b8276
PR feedback
Manvi-Agrawal Jul 26, 2024
9f4e5de
note about global phase
Manvi-Agrawal Jul 26, 2024
ad0d03a
pr feedback
Manvi-Agrawal Jul 26, 2024
49e4a80
Apply suggestions from code review
Manvi-Agrawal Jul 26, 2024
4fcc56d
Apply suggestions from code review
Manvi-Agrawal Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions samples/testing/operations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ There are two primary ways to test operations in the QDK:
- Use the `dump_operation` Python API to retrieve the operation's representation as a matrix and compare it against the expected matrix.

2. **Q# `Fact` Assertions:**
- Use a `Fact` function in your Q# code that uses the `CheckOperationsAreEqual` operation to verify if two operations are identical. The `Fact` function asserts that the check returns `true`.
- Use a `Fact` function in your Q# code that uses the `CheckOperationsAreEqual` operation to verify if two operations are identical upto a global phase. The `Fact` function asserts that the check returns `true`.
Manvi-Agrawal marked this conversation as resolved.
Show resolved Hide resolved

## Project Structure
This sample project is a multi-file Q# project that showcases both testing methods. The project structure is as follows:

- src
- `BellState.qs`: Q# file containing the `AllBellStates` operation to be tested
- `Test_SWAP.qs`: Q# file containing the `ApplySWAP1` and `ApplySWAP2` operations to be tested
- `OperationEquivalence.qs`: Q# file containing the `TestEquivalence` operation to be called in python wrapper
- `CustomOperation.qs`: Q# file containing the `ApplySWAP1` and `ApplySWAP2` operations to be tested
Manvi-Agrawal marked this conversation as resolved.
Show resolved Hide resolved
- `OperationEquivalence.qs`: Q# file containing the `TestEquivalence` operation to be called in Python wrapper
- `qsharp.json`: Q# project manifest file, instructing compiler to include all files in `src` directory.
- `test_dump_operation.py`: Python wrapper containing tests.

## Installation
- Install the `qsharp` python package by following the instructions mentioned [here](https://learn.microsoft.com/azure/quantum/install-overview-qdk#add-support-for-python-and-jupyter-notebooks).
- Install `pytest` python package.
- Install the `qsharp` Python package by following the instructions mentioned [here](https://learn.microsoft.com/azure/quantum/install-overview-qdk#add-support-for-python-and-jupyter-notebooks).
- Install `pytest` Python package.

## Running the sample
Open the `samples/testing/operations` directory, and run `pytest` command.
Expand Down
7 changes: 0 additions & 7 deletions samples/testing/operations/src/BellState.qs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/// # Sample
/// Multi File Testing Project (Refer to README for Project Overview)
///
/// # Description
/// This code builds upon the concepts explained in the README file,
/// demonstrating how to organize Q# code into multiple files for testing.

namespace BellState {
/// # Summary
/// Operation that generates all bell states for testing with `dump_operation.py`.
Expand Down
7 changes: 0 additions & 7 deletions samples/testing/operations/src/CustomOperation.qs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/// # Sample
/// Multi File Testing Project
///
/// # Description
/// This code builds upon the concepts explained in the README file,
/// demonstrating how to organize Q# code into multiple files for testing.

namespace CustomOperation {
// # Summary
/// CNOT based operation for testing with `TestEquivalence` operation.
Expand Down
11 changes: 2 additions & 9 deletions samples/testing/operations/src/OperationEquivalence.qs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/// # Sample
/// Multi File Testing Project (Refer to README for Project Overview)
///
/// # Description
/// This code builds upon the concepts explained in the README file,
/// demonstrating how to organize Q# code into multiple files for testing.

namespace OperationEquivalence {
open Microsoft.Quantum.Diagnostics;
open CustomOperation;
/// # Summary
/// Verifies the equivalence of quantum operations using `Fact` function
/// Verifies the equivalence of quantum operations upto a global phase using `Fact` function
Manvi-Agrawal marked this conversation as resolved.
Show resolved Hide resolved
/// and the `CheckOperationsAreEqual` operation. You can either run this here,
/// by clicking `Run` in VsCode or call `TestEquivalence` operation in python.
/// by clicking `Run` in VsCode or call `TestEquivalence` operation in Python.
@EntryPoint()
operation TestEquivalence() : Unit {
let actual = qs => CustomOperation.ApplySWAP(qs[0], qs[1]);
Expand Down
18 changes: 3 additions & 15 deletions samples/testing/operations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

@pytest.fixture(autouse=True)
def setup():
"""Fixture to execute asserts before a test is run"""
# You can set the target profile here if you want
# By default, it uses "Unrestricted" profile
qsharp.init(target_profile=qsharp.TargetProfile.Base)
"""Fixture to execute before a test is run"""
# Setting the project root to current folder.
qsharp.init(project_root=".")
yield # this is where the testing happens

def test_empty_operation() -> None:
Expand All @@ -22,26 +21,20 @@


def test_single_qubit_not_gate() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)

res = dump_operation("qs => X(qs[0])", 1)
assert res == [
[0, 1],
[1, 0],
]

def test_single_qubit_hadamard_gate() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)

res = dump_operation("qs => H(qs[0])", 1)
assert res == [
[0.707107, 0.707107],
[0.707107, -0.707107],
]

def test_two_qubit_cnot_gate() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)

res = dump_operation("qs => CNOT(qs[0], qs[1])", 2)
assert res == [
[1, 0, 0, 0],
Expand All @@ -51,10 +44,8 @@
]

def test_custom_operation() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)

qsharp.eval(
"operation ApplySWAP(qs : Qubit[]) : Unit is Ctl + Adj { SWAP(qs[0], qs[1]); }"
)

res = dump_operation("ApplySWAP", 2)
Expand All @@ -66,7 +57,6 @@
]

def test_operation_no_args_in_qsharp_file() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)
qsharp.init(project_root='.')
Manvi-Agrawal marked this conversation as resolved.
Show resolved Hide resolved

res = dump_operation("qs => CustomOperation.ApplySWAP(qs[0], qs[1])", 2)
Expand All @@ -78,7 +68,6 @@
]

def test_operation_with_args_in_qsharp_file() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)
qsharp.init(project_root='.')

res0 = dump_operation("BellState.AllBellStates(_, 0)", 2)
Expand Down Expand Up @@ -119,9 +108,8 @@


def test_operation_equivalence_using_fact() -> None:
qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted)
qsharp.init(project_root='.')

qsharp.eval(
"OperationEquivalence.TestEquivalence()"
)
Loading