This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Enable PrepareArbitraryState to use state injection on Quantum Simulator #370
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7e18170
Enable PrepareArbitraryState to use state injection on Quantum Simulator
ed3c208
Some minor CR feedback
705f6d8
Add dedicated operation for state preparation w/o Adj/Ctrl
cf84ea9
Merge branch 'main' into irinayat/nsim-state-prep
e4f3087
move to 0.14.20111301-pull QDK
29847a6
Update Standard/src/Preparation/Arbitrary.cs
IrinaYatsenko 74ea9ee
Comment, that the new API is for internal use only
IrinaYatsenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.