microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/testing/classical_values/test_classical_values.py

39lines · modeblame

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