Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

New kata functional programming techniques #452

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions FunctionalProgrammingTechniques/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"quantum.quantum-devkit-vscode"
]
}
36 changes: 36 additions & 0 deletions FunctionalProgrammingTechniques/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"args": [
"build"
],
"type": "process",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"label": "test",
"command": "dotnet",
"args": [
"test"
],
"type": "process",
"group": "test",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.12.20072031">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<IsPackable>false</IsPackable>
<RootNamespace>Quantum.Kata.BasicGates</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Xunit" Version="0.12.20072031" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<None Include="README.md" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionalProgrammingTechniques", "FunctionalProgrammingTechniques.csproj", "{4BAAB76A-ABFA-4F5C-9E5B-788BB3E8647E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4BAAB76A-ABFA-4F5C-9E5B-788BB3E8647E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BAAB76A-ABFA-4F5C-9E5B-788BB3E8647E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BAAB76A-ABFA-4F5C-9E5B-788BB3E8647E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BAAB76A-ABFA-4F5C-9E5B-788BB3E8647E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7CB0806C-1BBF-4E0C-BDFC-04A9A068F41C}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions FunctionalProgrammingTechniques/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Welcome!

The Functional Programming Techniques kata begins by covering the basics of using functional programming in Q# using classical operations and then adds Quantum operations to that foundation.

#### Theory

* The general theory behind functional programming can be found here: https://en.wikipedia.org/wiki/Functional_programming
* A tremendous introduction to the mathematics behind Category Theory and how Category Theory informs Functional Programming, see: https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/

#### Q# materials

* For Q#, the first thing to learn about functions is how they are manifest in Q# as either Functions for Operations: https://docs.microsoft.com/en-us/quantum/user-guide/using-qsharp/operations-functions
* Since Operations are intended for Quantum uses, they also support the Functors Controlled and Adjoint: https://docs.microsoft.com/en-us/quantum/user-guide/using-qsharp/operations-functions#controlled-and-adjoint-operations
* The next step is to understand how the higher-order functions are utilitized in the Quantum Libraries: https://docs.microsoft.com/en-us/quantum/user-guide/libraries/standard/control-flow
68 changes: 68 additions & 0 deletions FunctionalProgrammingTechniques/ReferenceImplementation.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all tasks.
// The tasks themselves can be found in Tasks.qs file.
// We recommend that you try to solve the tasks yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////

namespace Quantum.Kata.FunctionalProgrammingTechniques {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;

//TODO: copy in comments

function SimpleAdder_Reference ( addend: Int ) : Int {
return Add7 ( addend );
}

function Fold_Sum_Reference ( xs: Double[] ) : Double {
return Fold ( Sum, 0.0, xs );
}

function Fold_Product_Reference ( xs: Double[] ) : Double {
return Fold( Product, 1.0, xs);
}

function ComposeImpl_Reference( outerfn: (Double -> Double), innerfn: ( (Int, Int) -> Double ), fractionPiNumerator: Int, fractionPiDenominator: Int ) : Double {
return outerfn(innerfn(fractionPiNumerator, fractionPiDenominator));
}

function Compose_Reference(outerfn: (Double -> Double), innerfn: ( (Int, Int) -> Double )) : ( (Int, Int) -> Double ) {
return ComposeImpl_Reference(outerfn, innerfn, _, _);
}

function Compose_Functions_Reference (fractionPiNumerator: Int, fractionPiDenominator: Int) : Double {
let FractionPi_to_Degrees_Reference = Compose_Reference(Radians_to_Degrees, FractionPi_to_Radians);
return FractionPi_to_Degrees_Reference(fractionPiNumerator, fractionPiDenominator);
}

operation Uniform_Superposition_Reference ( qs: Qubit[] ) : Unit is Adj + Ctl {
ApplyToEachCA(H, qs);
}

operation Multiple_CNOT_Reference(qs: Qubit[]) : Unit is Adj + Ctl {
let nQubits = Length(qs);
ApplyToEachCA(CNOT, Zip(qs[0..nQubits -2], qs[1..nQubits - 1]));
}

operation IsConstantZeroConstant_Reference() : Bool {
return(IsOracleConstant(ConstantZero_Oracle));
}

operation IsConstantOneConstant_Reference() : Bool {
return(IsOracleConstant(ConstantOne_Oracle));
}

operation IsIdentityConstant_Reference() : Bool {
return(IsOracleConstant(Identity_Oracle));
}

operation IsNegationConstant_Reference() : Bool {
return(IsOracleConstant(Negation_Oracle));
}
}
Loading