microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9300733f19858fa2444c731bcda2faafddbd097e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/testing/classical_values/test_classical_values.py

42lines · modeblame

441fbd35Bill Ticehurst1 years ago1from pathlib import Path
897a397fMariia Mykhailova1 years ago2from random import randint
3import pytest
4import qsharp
5
441fbd35Bill Ticehurst1 years ago6
897a397fMariia Mykhailova1 years ago7@pytest.fixture(autouse=True)
8def setup():
9"""Fixture to execute before a test is run"""
10# Setting the project root to current folder.
441fbd35Bill Ticehurst1 years ago11this_dir = Path(__file__).parent
12qsharp.init(project_root=this_dir)
13yield # this is where the testing happens
897a397fMariia Mykhailova1 years ago14
15
16def test_classical_computation() -> None:
17"""Test that Q# code computes f(x) = x^2 correctly using Python test code."""
18for x in range(-10, 11):
19res = qsharp.eval(f"ClassicalFunction.Square({x})")
441fbd35Bill Ticehurst1 years ago20assert res == x**2
897a397fMariia Mykhailova1 years ago21
22
23def test_classical_computation_qsharp() -> None:
24"""Test that Q# code computes f(x) = x^2 correctly using Q# test code."""
25qsharp.eval("TestCode.TestSquare()")
26
27
28def test_measurement_results() -> None:
29"""Test that measuring a basis state returns correct measurement results using Python test code."""
30for _ in range(10):
31n = randint(2, 10)
32bits = [bool(randint(0, 1)) for _ in range(n)]
33# When passing Boolean values to Q#, remember to convert them to lowercase
34# (Python uses True and False, while Q# uses true and false)
35res = qsharp.eval(f"Measurement.MeasureBasisState({str(bits).lower()})")
36for i in range(n):
37assert (res[i] == qsharp.Result.One) == bits[i]
38
39
40def test_measurement_results_qsharp() -> None:
41"""Test that measuring a basis state returns correct measurement results using Q# test code."""
42qsharp.eval("TestCode.TestMeasurement()")