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
4 changes: 2 additions & 2 deletions Chemistry/src/Runtime/Runtime.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down Expand Up @@ -40,7 +40,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.13.201118141-beta" />
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.14.20111301-pull" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Chemistry/tests/ChemistryTests/QSharpTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<Import Project="..\..\..\Build\props\tests.props" />

Expand Down
2 changes: 1 addition & 1 deletion Chemistry/tests/SystemTests/SystemTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<Import Project="..\..\..\Build\props\tests.props" />

Expand Down
2 changes: 1 addition & 1 deletion MachineLearning/src/MachineLearning.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>Microsoft.Quantum.MachineLearning</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion MachineLearning/tests/MachineLearningTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<Import Project="..\..\Build\props\tests.props" />

Expand Down
4 changes: 2 additions & 2 deletions Numerics/src/Numerics.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.13.201118141-beta" />
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.14.20111301-pull" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Numerics/tests/NumericsTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<Import Project="..\..\Build\props\tests.props" />

Expand Down
82 changes: 82 additions & 0 deletions Standard/src/Preparation/Arbitrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Runtime.InteropServices;
using Microsoft.Quantum.Simulation;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;


namespace Microsoft.Quantum.Preparation
{
public partial class _PrepareAmplitudesFromZeroState
{
/// <summary>
/// Provides a native emulation of the ApproximatelyPrepareArbitraryState operation when
/// the operation is executed using the full-state QuantumSimulator.
/// </summary>
public class Native : _PrepareAmplitudesFromZeroState
{
[DllImport(QuantumSimulator.QSIM_DLL_NAME, ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl, EntryPoint = "InjectState")]
private static extern bool InjectState(uint sid, uint n, uint[] q, double[] re, double[] im);

private QuantumSimulator? Simulator { get; }

public Native(IOperationFactory m) : base(m)
{
this.Simulator = m as QuantumSimulator;
}

/// <summary>
/// Overrides the body to do the emulation when possible. If emulation is not possible, then
/// it just invokes the default Q# implementation.
/// </summary>
public override Func<(IQArray<Math.ComplexPolar>, Arithmetic.LittleEndian), QVoid>__Body__ => (_args) =>
{
var (polarAmplitudes, qubits) = _args;

// TODO: benchmark for small `qubits` arrays to find out in which cases emulation is actually
// beneficial.
if (this.Simulator == null)
{
return base.__Body__(_args);
}

// Calculate the norm as we might need to normalize the requsted state.
var norm = 0.0;
foreach (var pa in polarAmplitudes) { norm += pa.Magnitude * pa.Magnitude; }
norm = System.Math.Sqrt(norm);

// Setup the amplitudes arrays for the call to native (it needs to translate from polar to cartesian and
// might need to pad the tail of an incomplete amplitudes' array with zeros).
var stateSize = (long)1 << (int)qubits.Data.Length;
var re = new double[stateSize];
var im = new double[stateSize];
for (long i = 0; i < polarAmplitudes.Length; i++)
{
var pa = polarAmplitudes[i];
re[i] = (System.Math.Abs(pa.Magnitude) * System.Math.Cos(pa.Argument))/norm;
im[i] = (System.Math.Abs(pa.Magnitude) * System.Math.Sin(pa.Argument))/norm;
}
for (long i = polarAmplitudes.Length; i < stateSize; i++)
{
re[i] = 0.0;
im[i] = 0.0;
}

// Emulation might fail if the target qubits are entangled or not all in state |0>. In this case
// we should fallback to the quantum state preparation as it guarantees the operation to be a proper
// unitary no matter the state of the qubits.
if (!InjectState(Simulator.Id, (uint)qubits.Data.Length, qubits.Data.GetIds(), re, im))
{
return base.__Body__(_args);
}
return QVoid.Instance;
};
}
}
}
18 changes: 18 additions & 0 deletions Standard/src/Preparation/Arbitrary.qs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ namespace Microsoft.Quantum.Preparation {
ApproximatelyPrepareArbitraryState(0.0, coefficients, qubits);
}

/// # Summary
/// Given a set of coefficients and a little-endian encoded quantum register
/// of unentangled qubits, all of which are in zero state, prepares a state
/// on that register described by the given coefficients. Uses state emulation
/// if supported by the target.
///
/// # Notes
/// If the register isn't in zero state, the emulation will fail and fallback
/// to quantum state preparation.
/// This operation doesn't provide Adj/Ctr variants, because, in general, there
/// are no efficient emulation algorithms for those.
///
/// For internal use only, until proposal https://github.com/microsoft/qsharp-language/pull/41
/// is finalized and implemented.
operation _PrepareAmplitudesFromZeroState(coefficients : ComplexPolar[], qubits : LittleEndian) : Unit {
ApproximatelyPrepareArbitraryState(0.0, coefficients, qubits);
}

/// # Summary
/// Given a set of coefficients and a little-endian encoded quantum register,
/// prepares an state on that register described by the given coefficients,
Expand Down
4 changes: 2 additions & 2 deletions Standard/src/Standard.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down Expand Up @@ -37,7 +37,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.13.201118141-beta" />
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.14.20111301-pull" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="NumSharp" Version="0.20.5" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Standard/tests/Standard.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.13.201118141-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.14.20111301-pull">

<Import Project="..\..\Build\props\tests.props" />

Expand Down
4 changes: 2 additions & 2 deletions Visualization/src/Visualization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.13.201118141-beta" />
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.14.20111301-pull" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Quantum.IQSharp.Jupyter" Version="0.13.201118141-beta" PrivateAssets="All" />
<PackageReference Include="Microsoft.Quantum.IQSharp.Jupyter" Version="0.14.20111301-pull" PrivateAssets="All" />
<PackageReference Include="NumSharp" Version="0.20.5" />
</ItemGroup>

Expand Down