microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/testing/operations/test_operations.py
109lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | import pytest |
| 5 | import qsharp |
| 6 | from qsharp.utils import dump_operation |
| 7 | |
| 8 | @pytest.fixture(autouse=True) |
| 9 | def setup(): |
| 10 | """Fixture to execute before a test is run""" |
| 11 | # Setting the project root to current folder. |
| 12 | qsharp.init(project_root=".") |
| 13 | yield # this is where the testing happens |
| 14 | |
| 15 | def test_empty_operation() -> None: |
| 16 | res = dump_operation("qs => ()", 1) |
| 17 | assert res == [ |
| 18 | [1, 0], |
| 19 | [0, 1], |
| 20 | ] |
| 21 | |
| 22 | |
| 23 | def test_single_qubit_not_gate() -> None: |
| 24 | res = dump_operation("qs => X(qs[0])", 1) |
| 25 | assert res == [ |
| 26 | [0, 1], |
| 27 | [1, 0], |
| 28 | ] |
| 29 | |
| 30 | def test_single_qubit_hadamard_gate() -> None: |
| 31 | res = dump_operation("qs => H(qs[0])", 1) |
| 32 | assert res == [ |
| 33 | [0.707107, 0.707107], |
| 34 | [0.707107, -0.707107], |
| 35 | ] |
| 36 | |
| 37 | def test_two_qubit_cnot_gate() -> None: |
| 38 | res = dump_operation("qs => CNOT(qs[0], qs[1])", 2) |
| 39 | assert res == [ |
| 40 | [1, 0, 0, 0], |
| 41 | [0, 1, 0, 0], |
| 42 | [0, 0, 0, 1], |
| 43 | [0, 0, 1, 0], |
| 44 | ] |
| 45 | |
| 46 | def test_custom_operation() -> None: |
| 47 | qsharp.eval( |
| 48 | "operation ApplySWAP(qs : Qubit[]) : Unit is Ctl + Adj { SWAP(qs[0], qs[1]); }" |
| 49 | ) |
| 50 | |
| 51 | res = dump_operation("ApplySWAP", 2) |
| 52 | assert res == [ |
| 53 | [1, 0, 0, 0], |
| 54 | [0, 0, 1, 0], |
| 55 | [0, 1, 0, 0], |
| 56 | [0, 0, 0, 1], |
| 57 | ] |
| 58 | |
| 59 | def test_operation_no_args_in_qsharp_file() -> None: |
| 60 | res = dump_operation("qs => CustomOperation.ApplySWAP(qs[0], qs[1])", 2) |
| 61 | assert res == [ |
| 62 | [1, 0, 0, 0], |
| 63 | [0, 0, 1, 0], |
| 64 | [0, 1, 0, 0], |
| 65 | [0, 0, 0, 1], |
| 66 | ] |
| 67 | |
| 68 | def test_operation_with_args_in_qsharp_file() -> None: |
| 69 | res0 = dump_operation("BellState.AllBellStates(_, 0)", 2) |
| 70 | |
| 71 | assert res0 == [ |
| 72 | [0.707107, 0.0, 0.707107, 0.0], |
| 73 | [0.0, 0.707107, 0.0, 0.707107], |
| 74 | [0.0, 0.707107, 0.0, -0.707107], |
| 75 | [0.707107, 0.0, -0.707107, 0.0], |
| 76 | ] |
| 77 | |
| 78 | res1 = dump_operation("BellState.AllBellStates(_, 1)", 2) |
| 79 | |
| 80 | assert res1 == [ |
| 81 | [0.707107, 0.0, 0.707107, 0.0], |
| 82 | [0.0, 0.707107, 0.0, 0.707107], |
| 83 | [0.0, -0.707107, 0.0, 0.707107], |
| 84 | [-0.707107, 0.0, 0.707107, 0.0], |
| 85 | ] |
| 86 | |
| 87 | res2 = dump_operation("BellState.AllBellStates(_, 2)", 2) |
| 88 | |
| 89 | assert res2 == [ |
| 90 | [0.0, 0.707107, 0.0, 0.707107], |
| 91 | [0.707107, 0.0, 0.707107, 0.0], |
| 92 | [0.707107, 0.0, -0.707107, 0.0], |
| 93 | [0.0, 0.707107, 0.0, -0.707107], |
| 94 | ] |
| 95 | |
| 96 | res3 = dump_operation("BellState.AllBellStates(_, 3)", 2) |
| 97 | |
| 98 | assert res3 == [ |
| 99 | [0.0, 0.707107, 0.0, 0.707107], |
| 100 | [0.707107, 0.0, 0.707107, 0.0], |
| 101 | [-0.707107, 0.0, 0.707107, 0.0], |
| 102 | [0.0, -0.707107, 0.0, 0.707107], |
| 103 | ] |
| 104 | |
| 105 | |
| 106 | def test_operation_equivalence_using_fact() -> None: |
| 107 | qsharp.eval( |
| 108 | "OperationEquivalence.TestEquivalence()" |
| 109 | ) |
| 110 | |