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 90
Finish remaining decompositions for initial version of experimental simulators. #724
Merged
cgranade
merged 19 commits into
feature/experimental/opensim
from
cgranade/finish-opensim-decompositions
Jun 15, 2021
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0aa971f
Update documentation notebook for latest IQ# bugfix.
cgranade 554d6a9
Started work on decompositions.
cgranade b1fa50a
Added more detail to documentation notebook.
cgranade 0922442
Moved all decomposition logic to Q# operations.
cgranade 086c018
Added more decompositions.
cgranade 2225813
Make known limitations more explicit in README.md.
cgranade 06f03c2
Move known limitations to Markdown documentation./
cgranade fe89ad6
Started adding tests of decompositions.
cgranade 8de35ba
Added missing `open` statement.
cgranade 8083987
Don't mark decompositions as internal.
cgranade a65a853
Fixed build issue (💕 to @swernli for all the help!)
cgranade fda2e11
Unit tests now pass.
cgranade d29c99e
Address feedback.
cgranade 3591151
Fix two small build issues.
cgranade a950f16
Added comment explaining reset / release.
cgranade ad610f8
Remove DLL added by accident.
cgranade 1cc67a3
Removed one more file added by accident.
cgranade a369efe
Revert "Removed one more file added by accident."
cgranade 8c617a3
Added comment explaining vcomp140.dll.
cgranade 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
1,658 changes: 1,529 additions & 129 deletions
1,658
documentation/examples/experimental-simulators-from-python.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
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
199 changes: 199 additions & 0 deletions
199
src/Simulation/Simulators.Tests/OpenSystemsSimulatorTests/TestOperations.qs
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,199 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // # Design notes | ||
| // | ||
| // In testing the open systems simulator, we can't use the same approach used | ||
| // in testing other simulators. In particular, AssertMeasurement and | ||
| // AssertMeasurementProbability are no-ops on experimental simulators. Thus, | ||
| // we need to be a bit more indirect. | ||
| // | ||
| // Moreover, not all decompositions are supported yet | ||
| // (see documentation/experimental-simulators.md), such that we need to avoid | ||
| // unsupported cases. | ||
| // | ||
| // In this file, we list a bunch of operations that are unlikely to work | ||
| // correctly in the presence of decomposition bugs. This is not a guarantee, | ||
| // as we may have in the case of testing Choi–Jamilkowski states with | ||
| // assertions, but it should help build confidence in experimental simulator | ||
| // decompositions. | ||
| // | ||
| // In the future, consolidating these decompositions with those used in other | ||
| // targeting packages will allow using assertions on the full-state simulator | ||
| // to help build confidence in shared decompositions, further improving test | ||
| // coverage. | ||
|
|
||
| namespace Microsoft.Quantum.Experimental.Tests { | ||
| open Microsoft.Quantum.Intrinsic; | ||
| open Microsoft.Quantum.Diagnostics; | ||
|
|
||
| internal function Fact(expected : Bool, message : String) : Unit { | ||
| if not expected { | ||
| fail $"Fact was false: {message}"; | ||
| } | ||
| } | ||
|
|
||
| internal operation MX(target : Qubit) : Result { | ||
| return Measure([PauliX], [target]); | ||
| } | ||
|
|
||
| @Test("Microsoft.Quantum.Experimental.OpenSystemsSimulator") | ||
| operation CheckBellBasisParitiesWithSingleQubitMeasurements() : Unit { | ||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| Fact(M(left) == M(right), "Z parity in 00 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| Fact(MX(left) == MX(right), "X parity in 00 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| X(left); | ||
|
|
||
| Fact(M(left) != M(right), "Z parity in 10 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| X(left); | ||
|
|
||
| Fact(MX(left) == MX(right), "X parity in 10 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| Z(left); | ||
|
|
||
| Fact(M(left) == M(right), "Z parity in 01 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| Z(left); | ||
|
|
||
| Fact(MX(left) != MX(right), "X parity in 01 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| X(left); | ||
| Z(left); | ||
|
|
||
| Fact(M(left) != M(right), "Z parity in 11 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
|
|
||
| use (left, right) = (Qubit(), Qubit()) { | ||
| H(left); | ||
| CNOT(left, right); | ||
|
|
||
| X(left); | ||
| Z(left); | ||
|
|
||
| Fact(MX(left) != MX(right), "X parity in 11 case was wrong."); | ||
| ResetAll([left, right]); | ||
| } | ||
| } | ||
|
|
||
| internal function Xor(a : Bool, b : Bool) : Bool { | ||
| return (a or b) and ((not a) or (not b)); | ||
| } | ||
|
|
||
| @Test("Microsoft.Quantum.Experimental.OpenSystemsSimulator") | ||
| @Test("QuantumSimulator") // validate against full-state simulator. | ||
| operation CheckToffoliOnComputationalBasisStates() : Unit { | ||
| for in0 in [false, true] { | ||
| for in1 in [false, true] { | ||
| for output in [false, true] { | ||
| for useCcz in [false, true] { | ||
| use qs = Qubit[3]; | ||
| if in0 { X(qs[0]); } | ||
| if in1 { X(qs[1]); } | ||
| if output { X(qs[2]); } | ||
|
|
||
| let expectedOut = Xor(output, in0 and in1); | ||
|
|
||
| if useCcz { | ||
| within { | ||
| H(qs[2]); | ||
| } apply { | ||
| Controlled Z([qs[0], qs[1]], qs[2]); | ||
| } | ||
| } else { | ||
| Controlled X([qs[0], qs[1]], qs[2]); | ||
| } | ||
|
|
||
| let results = [M(qs[0]), M(qs[1]), M(qs[2])]; | ||
| let expected = [in0 ? One | Zero, in1 ? One | Zero, expectedOut ? One | Zero]; | ||
|
|
||
| Fact(results[0] == expected[0], $"in0 was incorrect in case: {in0} {in1} {output}. Got {results[0]}, expected {expected[0]}."); | ||
| Fact(results[1] == expected[1], $"in1 was incorrect in case: {in0} {in1} {output}. Got {results[1]}, expected {expected[1]}."); | ||
| Fact(results[2] == expected[2], $"expected was incorrect in case: {in0} {in1} {output}. Got {results[2]}, expected {expected[2]}."); | ||
|
|
||
| ResetAll(qs); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test("Microsoft.Quantum.Experimental.OpenSystemsSimulator") | ||
| @Test("QuantumSimulator") // validate against full-state simulator. | ||
| operation CheckXHSZSHIsNoOp() : Unit { | ||
| use q = Qubit(); | ||
|
|
||
| X(q); | ||
| within { | ||
| H(q); | ||
| S(q); | ||
| } apply { | ||
| Z(q); | ||
| } | ||
|
|
||
| Fact(M(q) == Zero, "XHSZSH was not a no-op."); | ||
| } | ||
|
|
||
| @Test("Microsoft.Quantum.Experimental.OpenSystemsSimulator") | ||
| @Test("QuantumSimulator") // validate against full-state simulator. | ||
| operation CheckControlledHWorks() : Unit { | ||
| use control = Qubit(); | ||
| use target = Qubit(); | ||
|
|
||
|
|
||
| Controlled H([control], target); | ||
| within { | ||
| X(control); | ||
| } apply { | ||
| Controlled H([control], target); | ||
| } | ||
| H(target); | ||
|
|
||
| Fact(M(control) == Zero, "Controlled H did not work correctly."); | ||
| Fact(M(target) == Zero, "Controlled H did not work correctly."); | ||
|
|
||
| } | ||
|
|
||
| } | ||
76 changes: 76 additions & 0 deletions
76
src/Simulation/Simulators/OpenSystemsSimulator/ContinuousTimeOperations.cs
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,76 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.Quantum.Simulation.Core; | ||
| using Microsoft.Quantum.Simulation.Common; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading; | ||
| using Microsoft.Quantum.Simulation.Simulators.Exceptions; | ||
| using Microsoft.Quantum.Intrinsic.Interfaces; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace Microsoft.Quantum.Experimental | ||
| { | ||
| public partial class OpenSystemsSimulator | ||
| { | ||
| // These gates are not yet supported, pending a design for how to extend | ||
| // noise models to continuous-time gates (that is, those parameterized | ||
| // by real numbers, such as angles). | ||
|
|
||
| void IIntrinsicExp.Body(IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| void IIntrinsicExp.AdjointBody(IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| void IIntrinsicExp.ControlledBody(IQArray<Qubit> controls, IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| void IIntrinsicExp.ControlledAdjointBody(IQArray<Qubit> controls, IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| void IIntrinsicR.Body(Pauli pauli, double angle, Qubit target) | ||
| { | ||
| if (pauli == Pauli.PauliI) | ||
| { | ||
| // Don't apply global phases on uncontrolled operations. | ||
| return; | ||
| } | ||
| throw new NotImplementedException("Arbitrary rotation with noise is not yet supported."); | ||
| } | ||
|
|
||
| void IIntrinsicR.AdjointBody(Pauli pauli, double angle, Qubit target) | ||
| { | ||
| (this as IIntrinsicR).Body(pauli, -angle, target); | ||
| } | ||
|
|
||
| void IIntrinsicR.ControlledBody(IQArray<Qubit> controls, Pauli pauli, double angle, Qubit target) | ||
| { | ||
| if (controls is { Count: 0 }) | ||
| { | ||
| (this as IIntrinsicR).Body(pauli, angle, target); | ||
| } | ||
| else | ||
| { | ||
| throw new NotImplementedException("Arbitrary controlled rotation with noise is not yet supported."); | ||
| } | ||
| } | ||
|
|
||
| void IIntrinsicR.ControlledAdjointBody(IQArray<Qubit> controls, Pauli pauli, double angle, Qubit target) | ||
| { | ||
| (this as IIntrinsicR).ControlledBody(controls, pauli, -angle, target); | ||
| } | ||
|
|
||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/Simulation/Simulators/OpenSystemsSimulator/Decompositions/ApplyWithLessControls.qs
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,40 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // NB: Copied from Utils.qs. | ||
| namespace Microsoft.Quantum.Experimental.Decompositions { | ||
| open Microsoft.Quantum.Experimental.Native as Native; | ||
|
|
||
| /// Given a multiply-controlled operation that requires k controls | ||
| /// applies it using ceiling(k/2) controls and using floor(k/2) temporary qubits | ||
| operation ApplyWithLessControlsA<'T> (op : ((Qubit[],'T) => Unit is Adj), (controls : Qubit[], arg : 'T)) : Unit is Adj { | ||
| let numControls = Length(controls); | ||
| let numControlPairs = numControls / 2; | ||
| use temps = Qubit[numControlPairs] { | ||
| within { | ||
| for numPair in 0 .. numControlPairs - 1 { // constant depth | ||
| PhaseCCX(controls[2 * numPair], controls[2 * numPair + 1], temps[numPair]); | ||
| } | ||
| } | ||
| apply { | ||
| let newControls = numControls % 2 == 0 ? temps | temps + [controls[numControls - 1]]; | ||
| op(newControls, arg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| operation PhaseCCX (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj { | ||
| // https://arxiv.org/pdf/1210.0974.pdf#page=2 | ||
| Native.H(target); | ||
| Native.CNOT(target,control1); | ||
| Native.CNOT(control1,control2); | ||
| Native.T(control2); | ||
| Adjoint Native.T(control1); | ||
| Native.T(target); | ||
| Native.CNOT(target,control1); | ||
| Native.CNOT(control1,control2); | ||
| Adjoint Native.T(control2); | ||
| Native.CNOT(target,control2); | ||
| Native.H(target); | ||
| } | ||
| } |
13 changes: 0 additions & 13 deletions
13
src/Simulation/Simulators/OpenSystemsSimulator/Decompositions/CZ.qs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.