Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Refs to Microsoft.Quantum in Samples and Katas #2030

Merged
merged 14 commits into from
Dec 4, 2024
Merged
  •  
  •  
  •  
102 changes: 61 additions & 41 deletions katas/content/KatasLibrary.qs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
// Licensed under the MIT License.

namespace Microsoft.Quantum.Katas {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
import KatasUtils.*;
export
CheckOperationsAreEqualStrict,
CheckOperationsEquivalenceOnZeroState,
CheckOperationsEquivalenceOnInitialStateStrict,
CheckOperationsEquivalenceOnZeroStateStrict,
ShowQuantumStateComparison,
CheckOperationsEquivalenceOnZeroStateWithFeedback,
EntangleRegisters,
PrepDemoState,
DistinguishTwoStates_SingleQubit,
DistinguishStates_MultiQubit,
BoolArrayAsKetState,
IntArrayAsStateName,
CheckOracleImplementsFunction;
}

namespace KatasUtils {
import Std.Arrays.*;
import Std.Convert.*;
import Std.Diagnostics.*;
import Std.Math.*;
import Std.Random.*;

/// # Summary
/// Given two operations, checks whether they act identically (including global phase) for all input states.
Expand All @@ -15,8 +33,8 @@ namespace Microsoft.Quantum.Katas {
operation CheckOperationsAreEqualStrict(
inputSize : Int,
op : (Qubit[] => Unit is Adj + Ctl),
reference : (Qubit[] => Unit is Adj + Ctl))
: Bool {
reference : (Qubit[] => Unit is Adj + Ctl)
) : Bool {
Fact(inputSize > 0, "`inputSize` must be positive");
let controlledOp = register => Controlled op(register[...0], register[1...]);
let controlledReference = register => Controlled reference(register[...0], register[1...]);
Expand All @@ -30,8 +48,8 @@ namespace Microsoft.Quantum.Katas {
operation CheckOperationsEquivalenceOnZeroState(
op : (Qubit[] => Unit),
reference : (Qubit[] => Unit is Adj),
inputSize : Int)
: Bool {
inputSize : Int
) : Bool {
Fact(inputSize > 0, "`inputSize` must be positive");
use target = Qubit[inputSize];
op(target);
Expand All @@ -57,8 +75,7 @@ namespace Microsoft.Quantum.Katas {
within {
H(control);
initialState(target);
}
apply {
} apply {
Controlled op([control], target);
Adjoint Controlled reference([control], target);
}
Expand All @@ -77,8 +94,8 @@ namespace Microsoft.Quantum.Katas {
operation CheckOperationsEquivalenceOnZeroStateStrict(
op : (Qubit[] => Unit is Adj + Ctl),
reference : (Qubit[] => Unit is Adj + Ctl),
inputSize : Int)
: Bool {
inputSize : Int
) : Bool {
Fact(inputSize > 0, "`inputSize` must be positive");
CheckOperationsEquivalenceOnInitialStateStrict(qs => (), op, reference, inputSize)
}
Expand All @@ -91,8 +108,8 @@ namespace Microsoft.Quantum.Katas {
registerSize : Int,
initialState : Qubit[] => Unit,
op : Qubit[] => Unit,
reference : Qubit[] => Unit)
: Unit {
reference : Qubit[] => Unit
) : Unit {
{
use register = Qubit[registerSize];
initialState(register);
Expand Down Expand Up @@ -142,10 +159,12 @@ namespace Microsoft.Quantum.Katas {

internal operation EntangleRegisters(
control : Qubit[],
target : Qubit[]) : Unit is Adj + Ctl {
target : Qubit[]
) : Unit is Adj + Ctl {
Fact(
Length(control) == Length(target),
$"The length of qubit registers must be the same.");
$"The length of qubit registers must be the same."
);

for index in IndexRange(control) {
H(control[index]);
Expand All @@ -158,7 +177,7 @@ namespace Microsoft.Quantum.Katas {
/// Prepare a random uneven superposition state on the given qubit array.
operation PrepDemoState(qs : Qubit[]) : Unit {
Fact(Length(qs) <= 4, "States with 5 qubits or more are not supported.");
let probs = [0.36, 0.25, 1. / 3., 1. / 5.][... Length(qs) - 1];
let probs = [0.36, 0.25, 1. / 3., 1. / 5.][...Length(qs) - 1];
for (q, prob) in Zipped(qs, probs) {
Ry(ArcCos(Sqrt(prob)) * 2.0, q);
}
Expand All @@ -171,14 +190,15 @@ namespace Microsoft.Quantum.Katas {
statePrep : ((Qubit, Int) => Unit is Adj),
testImpl : (Qubit => Bool),
stateNames : String[],
preserveState : Bool) : Bool {
preserveState : Bool
) : Bool {

let nTotal = 100;
let nStates = 2;
mutable misclassifications = [0, size=nStates];
mutable misclassifications = [0, size = nStates];

use q = Qubit();
for _ in 1 .. nTotal {
for _ in 1..nTotal {
// get a random bit to define whether qubit will be in a state corresponding to true return (1) or to false one (0)
// state = 0 false return
// state = 1 true return
Expand Down Expand Up @@ -207,7 +227,7 @@ namespace Microsoft.Quantum.Katas {
}

mutable totalMisclassifications = 0;
for i in 0 .. nStates - 1 {
for i in 0..nStates - 1 {
if misclassifications[i] != 0 {
set totalMisclassifications += misclassifications[i];
Message($"Misclassified {stateNames[i]} as {stateNames[1 - i]} in {misclassifications[i]} test runs.");
Expand All @@ -226,7 +246,8 @@ namespace Microsoft.Quantum.Katas {
statePrep : ((Qubit[], Int, Double) => Unit is Adj),
testImpl : (Qubit[] => Int),
preserveState : Bool,
stateNames : String[]) : Bool {
stateNames : String[]
) : Bool {

let nTotal = 100;
// misclassifications will store the number of times state i has been classified as state j (dimension nStates^2)
Expand All @@ -235,7 +256,7 @@ namespace Microsoft.Quantum.Katas {
mutable unknownClassifications = [0, size = nStates];

use qs = Qubit[nQubits];
for _ in 1 .. nTotal {
for _ in 1..nTotal {
// get a random integer to define the state of the qubits
let state = DrawRandomInt(0, nStates - 1);
// get a random rotation angle to define the exact state of the qubits
Expand All @@ -252,8 +273,7 @@ namespace Microsoft.Quantum.Katas {
if ans != state {
set misclassifications w/= ((state * nStates) + ans) <- (misclassifications[(state * nStates) + ans] + 1);
}
}
else {
} else {
// classification result is an invalid state index - file it separately
set unknownClassifications w/= state <- (unknownClassifications[state] + 1);
}
Expand All @@ -273,8 +293,8 @@ namespace Microsoft.Quantum.Katas {
}

mutable totalMisclassifications = 0;
for i in 0 .. nStates - 1 {
for j in 0 .. nStates - 1 {
for i in 0..nStates - 1 {
for j in 0..nStates - 1 {
if misclassifications[(i * nStates) + j] != 0 {
set totalMisclassifications += misclassifications[i * nStates + j];
Message($"Misclassified {stateNames[i]} as {stateNames[j]} in {misclassifications[(i * nStates) + j]} test runs.");
Expand All @@ -289,22 +309,22 @@ namespace Microsoft.Quantum.Katas {
}

// Helper function to convert a boolean array to its ket state representation
function BoolArrayAsKetState (bits : Bool[]) : String {
function BoolArrayAsKetState(bits : Bool[]) : String {
mutable stateName = "|";
for i in 0 .. Length(bits) - 1 {
for i in 0..Length(bits) - 1 {
set stateName += (bits[i] ? "1" | "0");
}

return stateName + "⟩";
}

// Helper function to convert an array of bit strings to its ket state representation
function IntArrayAsStateName (
function IntArrayAsStateName(
qubits : Int,
bitStrings : Bool[][]
) : String {
mutable statename = "";
for i in 0 .. Length(bitStrings) - 1 {
for i in 0..Length(bitStrings) - 1 {
if i > 0 {
set statename += " + ";
}
Expand All @@ -315,25 +335,25 @@ namespace Microsoft.Quantum.Katas {
}

/// # Summary
/// Given a marking oracle acting on N inputs, and a classical function acting on N bits,
/// Given a marking oracle acting on N inputs, and a classical function acting on N bits,
/// checks whether the oracle effect matches that of the function on every classical input.
operation CheckOracleImplementsFunction (
N : Int,
oracle : (Qubit[], Qubit) => Unit,
operation CheckOracleImplementsFunction(
N : Int,
oracle : (Qubit[], Qubit) => Unit,
f : Bool[] -> Bool
) : Bool {
let size = 1 <<< N;
use (input, target) = (Qubit[N], Qubit());
for k in 0 .. size - 1 {
for k in 0..size - 1 {
// Prepare k-th bit vector
let binaryLE = IntAsBoolArray(k, N);

// "binary" is little-endian notation, so the second vector tried has qubit 0 in state 1 and the rest in state 0
ApplyPauliFromBitString(PauliX, true, binaryLE, input);

// Apply the operation
oracle(input, target);

// Calculate the expected classical result
let val = f(binaryLE);

Expand All @@ -357,5 +377,5 @@ namespace Microsoft.Quantum.Katas {
}
}
return true;
}
}
}
6 changes: 3 additions & 3 deletions katas/content/complex_arithmetic/Common.qs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Convert;
import Std.Math.*;
import Std.Random.*;
import Std.Convert.*;

operation DrawRandomComplex() : Complex {
// Generates a random complex number.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexToComplexPolar(x : Complex) : ComplexPolar {
// Implement your solution here...
return ComplexPolar(0., 0.);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexToComplexPolar(x : Complex) : ComplexPolar {
let (a, b) = (x.Real, x.Imag);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace Kata.Verification {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
import Std.Convert.*;
import Std.Math.*;

@EntryPoint()
operation CheckSolution() : Bool {
for _ in 0 .. 24 {
operation CheckSolution() : Bool {
for _ in 0..24 {
let x = DrawRandomComplex();

let expected = ComplexAsComplexPolar(x);
let actual = Kata.ComplexToComplexPolar(x);

if not ComplexPolarEqual(expected, actual) {
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {ComplexPolarAsString(expected)}, actual return {ComplexPolarAsString(actual)}.");
Expand All @@ -20,4 +20,4 @@ namespace Kata.Verification {
Message("Correct!");
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Return the polar representation of $x = re^{i\theta}$, that is, the distance fro

A video explanation of this conversion can be found [here](https://www.youtube.com/watch?v=8RasCV_Lggg).

Q# namespace `Microsoft.Quantum.Math` includes a useful function `ArcTan2()`.
Q# namespace `Std.Math` includes a useful function `ArcTan2()`.

</details>

> Q# function `ComplexAsComplexPolar` from `Microsoft.Quantum.Math` namespace converts a complex number of type `Complex` to a complex number of type `ComplexPolar`. For educational purposes, try to do this task by hand.
> Q# function `ComplexAsComplexPolar` from `Std.Math` namespace converts a complex number of type `Complex` to a complex number of type `ComplexPolar`. For educational purposes, try to do this task by hand.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexAdd(x : Complex, y : Complex) : Complex {
// Extract real and imaginary components of the inputs.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexAdd(x : Complex, y : Complex) : Complex {
Complex(x.Real + y.Real, x.Imag + y.Imag)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
import Std.Math.*;

@EntryPoint()
operation CheckSolution() : Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Adding complex numbers is just like adding polynomials. Add components of the sa
A video explanation of adding complex numbers can be found [here](https://www.youtube.com/watch?v=SfbjqVyQljk).
</details>

> Q# function `PlusC` from `Microsoft.Quantum.Math` namespace adds two complex numbers. For educational purposes, try to do this task by hand.
> Q# function `PlusC` from `Std.Math` namespace adds two complex numbers. For educational purposes, try to do this task by hand.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexConjugate(x : Complex) : Complex {
// Implement your solution here...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexConjugate(x : Complex) : Complex {
Complex(x.Real, -x.Imag)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexConjugate_Reference(x : Complex) : Complex {
// Return the complex conjugate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexDiv(x : Complex, y : Complex) : Complex {
// Implement your solution here...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexDiv(x : Complex, y : Complex) : Complex {
let (a, b) = (x.Real, x.Imag);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata.Verification {
open Microsoft.Quantum.Math;
import Std.Math.*;

@EntryPoint()
operation CheckSolution() : Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ A video explanation of complex division can be found [here](https://www.youtube.

</details>

> Q# function `DividedByC` from `Microsoft.Quantum.Math` namespace divides two complex numbers. For educational purposes, try to do this task by hand.
> Q# function `DividedByC` from `Std.Math` namespace divides two complex numbers. For educational purposes, try to do this task by hand.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Kata {
open Microsoft.Quantum.Math;
import Std.Math.*;

function ComplexExponent(x : Complex) : Complex {
// Implement your solution here...
Expand Down
Loading
Loading