microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
132013d5c64d73afbbff73d0c0b3ee545f795e9b

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/testing/classical_values/src/TestCode.qs

27lines · modecode

1import Std.Diagnostics.Fact;
2import Std.Arrays.*;
3import Std.Random.*;
4import ClassicalFunction.Square;
5import Measurement.MeasureBasisState;
6
7/// # Summary
8/// Test code that verifies the classical values returned by the rest of the code.
9/// Throw exceptions if the test fails.
10
11function TestSquare() : Unit {
12 for i in -10..10 {
13 let (actual, expected) = (Square(i), i * i);
14 Fact(actual == expected, $"Incorrect function value for {i}: expected {expected}, got {actual}");
15 }
16}
17
18operation TestMeasurement() : Unit {
19 for _ in 1..10 {
20 let n = DrawRandomInt(2, 10);
21 let bits = ForEach(x => DrawRandomBool(0.5), [0, size = n]);
22 let res = MeasureBasisState(bits);
23 for (bit, resBit) in Zipped(bits, res) {
24 Fact(bit == (resBit == One), $"Incorrect measurement result for {bit}");
25 }
26 }
27}