Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,4 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/
/src/Simulation/Simulators.Tests/TestProjects/QsharpExe/built
22 changes: 22 additions & 0 deletions Simulation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Tests.RoslynWrapper", "src\
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Microsoft.Quantum.CsharpGeneration", "src\Simulation\CsharpGeneration\Microsoft.Quantum.CsharpGeneration.fsproj", "{B96E97F4-2DC8-45AC-ADF5-861D0D3073FC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestProjects", "TestProjects", "{09C842CB-930C-4C7D-AD5F-E30DE4A55820}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QsharpExe", "src\Simulation\Simulators.Tests\TestProjects\QsharpExe\QsharpExe.csproj", "{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -239,6 +243,22 @@ Global
{B96E97F4-2DC8-45AC-ADF5-861D0D3073FC}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{B96E97F4-2DC8-45AC-ADF5-861D0D3073FC}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{B96E97F4-2DC8-45AC-ADF5-861D0D3073FC}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Debug|x64.ActiveCfg = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Debug|x64.Build.0 = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Release|Any CPU.Build.0 = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Release|x64.ActiveCfg = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.Release|x64.Build.0 = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -256,6 +276,8 @@ Global
{618FBF9D-4EF3-435D-9728-81C726236668} = {A567C185-A429-418B-AFDE-6F1785BA4A77}
{48206BD6-48DD-4442-A395-3A6594E4C9C6} = {A567C185-A429-418B-AFDE-6F1785BA4A77}
{B96E97F4-2DC8-45AC-ADF5-861D0D3073FC} = {A567C185-A429-418B-AFDE-6F1785BA4A77}
{09C842CB-930C-4C7D-AD5F-E30DE4A55820} = {34D419E9-CCF1-4E48-9FA4-3AD4B86BEEB4}
{2F5796A7-4AF8-4B78-928A-0A3A80752F9D} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {929C0464-86D8-4F70-8835-0A5EAF930821}
Expand Down
54 changes: 36 additions & 18 deletions src/Simulation/Simulators.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;

using System.Reflection;
using System.Text;
using Microsoft.Quantum.QsCompiler;
using Microsoft.Quantum.Simulation.Common;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
Expand All @@ -14,8 +17,6 @@

namespace Microsoft.Quantum.Simulation.Simulators.Tests
{
using Helper = Microsoft.Quantum.Simulation.Simulators.Tests.OperationsTestHelper;

public class CoreTests
{
private readonly ITestOutputHelper output;
Expand All @@ -25,10 +26,27 @@ public CoreTests(ITestOutputHelper output)
this.output = output;
}

[Fact]
public void BasicExecution()
{
var asmPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var exe = Path.Combine(asmPath, "TestExe", "QsharpExe.exe");

ProcessRunner.Run(exe, "", out var _, out StringBuilder error, out int exitCode, out Exception ex);
Assert.Null(ex);
Assert.Equal(1, exitCode);
Assert.Contains("NotImplementedException", error.ToString());

ProcessRunner.Run(exe, "--simulator QuantumSimulator", out var _, out error, out exitCode, out ex);
Assert.Null(ex);
Assert.Equal(0, exitCode);
Assert.Empty(error.ToString().Trim());
}

[Fact]
public void Borrowing()
{
Helper.RunWithMultipleSimulators((s) =>
OperationsTestHelper.RunWithMultipleSimulators((s) =>
{
var tracker = new StartTracker(s);

Expand All @@ -44,18 +62,18 @@ public void Borrowing()

BorrowingTest.Run(s).Wait();

var tracer = Helper.GetTracer<(long, Qubit)>(s);
var tracer = OperationsTestHelper.GetTracer<(long, Qubit)>(s);

var testOne = new System.Action<int, OperationFunctor, (int, Qubit)>((callsCount, variant, info) =>
var testOne = new Action<int, OperationFunctor, (int, Qubit)>((int callsCount, OperationFunctor variant, (int, Qubit) info) =>
{
var (available, q) = info;
Assert.Equal(callsCount, tracer.Log.GetNumberOfCalls(variant, (available, q)));
});

var testOneBody = new System.Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.Body, info));
var testOneAdjoint = new System.Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.Adjoint, info));
var testOneCtrl = new System.Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.Controlled, info));
var testOneCtrlAdj = new System.Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.ControlledAdjoint, info));
var testOneBody = new Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.Body, info));
var testOneAdjoint = new Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.Adjoint, info));
var testOneCtrl = new Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.Controlled, info));
var testOneCtrlAdj = new Action<int, (int, Qubit)>((callsCount, info) => testOne(callsCount, OperationFunctor.ControlledAdjoint, info));

testOneBody(6, (0, q5));
testOneBody(6, (0, q6));
Expand Down Expand Up @@ -180,7 +198,7 @@ void RunOne(IOperationFactory s)
Assert.False(File.Exists("()"));
}

Helper.RunWithMultipleSimulators((s) => RunOne(s as IOperationFactory));
OperationsTestHelper.RunWithMultipleSimulators((s) => RunOne(s as IOperationFactory));
RunOne(new QCTraceSimulator());
RunOne(new ResourcesEstimator());
RunOne(new QuantumSimulator());
Expand Down Expand Up @@ -215,17 +233,17 @@ void RunOne(IOperationFactory s)
}
}

Helper.RunWithMultipleSimulators((s) => RunOne(s as IOperationFactory));
OperationsTestHelper.RunWithMultipleSimulators((s) => RunOne(s as IOperationFactory));
RunOne(new QCTraceSimulator());
RunOne(new ResourcesEstimator());
}

[Fact]
public void ZeroQubits()
{
Helper.RunWithMultipleSimulators((s) =>
OperationsTestHelper.RunWithMultipleSimulators((s) =>
{
var tracer = Helper.GetTracer<string>(s);
var tracer = OperationsTestHelper.GetTracer<string>(s);

ZeroQubitsTest.Run(s).Wait();

Expand All @@ -236,7 +254,7 @@ public void ZeroQubits()
[Fact]
public void InterpolatedStrings()
{
Helper.RunWithMultipleSimulators((s) =>
OperationsTestHelper.RunWithMultipleSimulators((s) =>
{
Circuits.InterpolatedStringTest.Run(s).Wait(); // Throws if it doesn't succeed
});
Expand All @@ -245,7 +263,7 @@ public void InterpolatedStrings()
[Fact]
public void RandomOperation()
{
Helper.RunWithMultipleSimulators((s) =>
OperationsTestHelper.RunWithMultipleSimulators((s) =>
{
Circuits.RandomOperationTest.Run(s).Wait(); // Throws if it doesn't succeed
});
Expand All @@ -254,7 +272,7 @@ public void RandomOperation()
[Fact]
public void BigInts()
{
Helper.RunWithMultipleSimulators((s) =>
OperationsTestHelper.RunWithMultipleSimulators((s) =>
{
Circuits.BigIntTest.Run(s).Wait(); // Throws if it doesn't succeed
});
Expand Down Expand Up @@ -282,6 +300,6 @@ public void CatchFail()

[Fact]
public void InternalCallables() =>
Helper.RunWithMultipleSimulators(s => Circuits.InternalCallablesTest.Run(s).Wait());
OperationsTestHelper.RunWithMultipleSimulators(s => Circuits.InternalCallablesTest.Run(s).Wait());
}
}
16 changes: 16 additions & 0 deletions src/Simulation/Simulators.Tests/TestProjects/QsharpExe/Main.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace QsharpExe {

open Microsoft.Quantum.Intrinsic;

@EntryPoint()
operation Main () : Result {
using (q = Qubit()) {
H(q);
let res = M(q);
if (res == One) {
X(q);
}
return res;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.10.2004.1717-alpha">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<!-- we will provide our own -->
<CsharpGeneration>false</CsharpGeneration>
<IncludeQsharpCorePackages>false</IncludeQsharpCorePackages>
<DefaultSimulator>ToffoliSimulator</DefaultSimulator>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\QsharpCore\Microsoft.Quantum.QSharp.Core.csproj" />
<ProjectReference Include="..\..\..\CsharpGeneration\Microsoft.Quantum.CsharpGeneration.fsproj" PrivateAssets="All" IsQscReference="true" />
<ProjectReference Include="..\..\..\Simulators\Microsoft.Quantum.Simulation.Simulators.csproj" />
</ItemGroup>

<Target Name="BeforeCsharpCompile">
<ItemGroup>
<Compile Include="$(GeneratedFilesOutputPath)**/*.g.cs" Exclude="@(Compile)" AutoGen="true" />
</ItemGroup>
</Target>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<PropertyGroup>
<_ExeDir>$(MSBuildThisFileDirectory)built</_ExeDir>
</PropertyGroup>
<ItemGroup>
<FilesToRemove Include="$(_ExeDir)**" />
</ItemGroup>
<Delete Files="@(_ExeDir)" />
<MakeDir Directories="$(_ExeDir)" />
<ItemGroup>
<_ExeFiles Include="$(OutputPath)*" />
</ItemGroup>
<Copy SourceFiles="@(_ExeFiles)" DestinationFolder="$(_ExeDir)" ContinueOnError="false" />
</Target>

</Project>

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="TestProjects\**" />
<EmbeddedResource Remove="TestProjects\**" />
<QsharpCompile Remove="TestProjects\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Xunit\Microsoft.Quantum.Xunit.csproj" />
<ProjectReference Include="..\CsharpGeneration\Microsoft.Quantum.CsharpGeneration.fsproj" IsQscReference="true" />
<ProjectReference Include="..\CsharpGeneration\Microsoft.Quantum.CsharpGeneration.fsproj" IsQscReference="true" />
<ProjectReference Include="TestProjects\QsharpExe\QsharpExe.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

<ItemGroup>
Expand All @@ -24,7 +33,18 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<PropertyGroup>
<_ExeDir>$(MSBuildThisFileDirectory)TestProjects\QsharpExe\built\</_ExeDir>
</PropertyGroup>
<ItemGroup>
<_ExeFiles Include="$(_ExeDir)*" />
</ItemGroup>
<MakeDir Directories="$(OutputPath)TestExe" />
<Copy SourceFiles="@(_ExeFiles)" DestinationFolder="$(OutputPath)TestExe" ContinueOnError="false" />
</Target>

</Project>

Expand Down