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