-
Notifications
You must be signed in to change notification settings - Fork 179
Fix #546 #553
Fix #546 #553
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,33 +10,20 @@ namespace Microsoft.Quantum.MachineLearning.Tests { | |
|
|
||
| @Test("QuantumSimulator") | ||
| function NQubitsRequiredFact() : Unit { | ||
| let model = Default<ML.SequentialModel>() | ||
| w/ Structure <- [ | ||
| ML.ControlledRotation((3, [7, 9]), PauliX, 0), | ||
| ML.ControlledRotation((8, []), PauliY, 1) | ||
| ]; | ||
| let model = ML.SequentialModel([ | ||
| ML.ControlledRotation((3, [7, 9]), PauliX, 0), | ||
| ML.ControlledRotation((8, []), PauliY, 1) | ||
| ], [], 0.); | ||
| let actual = ML.NQubitsRequired(model); | ||
| EqualityFactI(actual, 10, "Wrong output from NQubitsRequired."); | ||
| } | ||
|
|
||
| function ExampleModel() : ML.SequentialModel { | ||
| return Default<ML.SequentialModel>() | ||
| w/ Structure <- [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here, I think the original code was more readable even if less correct... from that perspective, I agree with your change but would like to kick off a discussion on more readable alternatives going forward?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the original code was more readable. Do you suggest to keep the version using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think getting rid of |
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 2 | ||
| w/ ControlIndices <- [0] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 0, | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 0 | ||
| w/ ControlIndices <- [1, 2] | ||
| w/ Axis <- PauliZ | ||
| w/ ParameterIndex <- 1 | ||
| ] | ||
| w/ Parameters <- [ | ||
| 1.234, | ||
| 2.345 | ||
| ]; | ||
| return ML.SequentialModel([ | ||
| ML.ControlledRotation((2, [0]), PauliX, 0), | ||
| ML.ControlledRotation((0, [1, 2]), PauliZ, 1)], | ||
| [1.234, 2.345], | ||
| 0.0); | ||
| } | ||
|
|
||
| operation ApplyExampleModelManually(register : Qubit[]) : Unit is Adj + Ctl { | ||
|
|
@@ -64,23 +51,9 @@ namespace Microsoft.Quantum.MachineLearning.Tests { | |
| Fact(All(EqualCR, Zipped( | ||
| ML.LocalRotationsLayer(3, PauliY), | ||
| [ | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 0 | ||
| w/ ControlIndices <- [] | ||
| w/ Axis <- PauliY | ||
| w/ ParameterIndex <- 0, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 1 | ||
| w/ ControlIndices <- [] | ||
| w/ Axis <- PauliY | ||
| w/ ParameterIndex <- 1, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 2 | ||
| w/ ControlIndices <- [] | ||
| w/ Axis <- PauliY | ||
| w/ ParameterIndex <- 2 | ||
| ML.ControlledRotation((0, []), PauliY, 0), | ||
| ML.ControlledRotation((1, []), PauliY, 1), | ||
| ML.ControlledRotation((2, []), PauliY, 2) | ||
| ] | ||
| )), "LocalRotationsLayer returned wrong output."); | ||
| } | ||
|
|
@@ -90,23 +63,9 @@ namespace Microsoft.Quantum.MachineLearning.Tests { | |
| Fact(All(EqualCR, Zipped( | ||
| ML.PartialRotationsLayer([4, 5, 6], PauliY), | ||
| [ | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 4 | ||
| w/ ControlIndices <- [] | ||
| w/ Axis <- PauliY | ||
| w/ ParameterIndex <- 0, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 5 | ||
| w/ ControlIndices <- [] | ||
| w/ Axis <- PauliY | ||
| w/ ParameterIndex <- 1, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 6 | ||
| w/ ControlIndices <- [] | ||
| w/ Axis <- PauliY | ||
| w/ ParameterIndex <- 2 | ||
| ML.ControlledRotation((4, []), PauliY, 0), | ||
| ML.ControlledRotation((5, []), PauliY, 1), | ||
| ML.ControlledRotation((6, []), PauliY, 2) | ||
| ] | ||
| )), "PartialRotationsLayer returned wrong output."); | ||
| } | ||
|
|
@@ -116,23 +75,9 @@ namespace Microsoft.Quantum.MachineLearning.Tests { | |
| Fact(All(EqualCR, Zipped( | ||
| ML.CyclicEntanglingLayer(3, PauliX, 2), | ||
| [ | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 0 | ||
| w/ ControlIndices <- [2] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 0, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 1 | ||
| w/ ControlIndices <- [0] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 1, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 2 | ||
| w/ ControlIndices <- [1] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 2 | ||
| ML.ControlledRotation((0, [2]), PauliX, 0), | ||
| ML.ControlledRotation((1, [0]), PauliX, 1), | ||
| ML.ControlledRotation((2, [1]), PauliX, 2) | ||
| ] | ||
| )), "CyclicEntanglingLayer returned wrong output."); | ||
| } | ||
|
|
@@ -141,46 +86,19 @@ namespace Microsoft.Quantum.MachineLearning.Tests { | |
| function CombinedStructureFact() : Unit { | ||
| let combined = ML.CombinedStructure([ | ||
| [ | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 0 | ||
| w/ ControlIndices <- [2] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 0, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 1 | ||
| w/ ControlIndices <- [0] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 1 | ||
| ML.ControlledRotation((0, [2]), PauliX, 0), | ||
| ML.ControlledRotation((1, [0]), PauliX, 1) | ||
| ], | ||
| [ | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 2 | ||
| w/ ControlIndices <- [1] | ||
| w/ Axis <- PauliZ | ||
| w/ ParameterIndex <- 0 | ||
| ML.ControlledRotation((2, [1]), PauliZ, 0) | ||
| ] | ||
| ]); | ||
| Fact(All(EqualCR, Zipped( | ||
| combined, | ||
| [ | ||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 0 | ||
| w/ ControlIndices <- [2] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 0, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 1 | ||
| w/ ControlIndices <- [0] | ||
| w/ Axis <- PauliX | ||
| w/ ParameterIndex <- 1, | ||
|
|
||
| Default<ML.ControlledRotation>() | ||
| w/ TargetIndex <- 2 | ||
| w/ ControlIndices <- [1] | ||
| w/ Axis <- PauliZ | ||
| w/ ParameterIndex <- 2 | ||
| ML.ControlledRotation((0, [2]), PauliX, 0), | ||
| ML.ControlledRotation((1, [0]), PauliX, 1), | ||
| ML.ControlledRotation((2, [1]), PauliZ, 2) | ||
| ] | ||
| )), "CombinedStructure returned wrong output."); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,8 @@ namespace Microsoft.Quantum.Arrays { | |
| /// ``` | ||
| function CumulativeFolded<'State, 'T>(fn : (('State, 'T) -> 'State), state : 'State, array : 'T[]) : 'State[] { | ||
| mutable current = state; | ||
| mutable result = [Default<'State>(), size = Length(array)]; | ||
| // initialize with current, and then overwrite in loop | ||
| mutable result = [current, size = Length(array)]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need an early exit if
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is implied by
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, wasn't entirely sure from a readthrough so wanted to double-check. Thanks! |
||
|
|
||
| for (i, elem) in Enumerated(array) { | ||
| set current = fn(current, elem); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be worth an extended discussion. Before deprecating
Default, it was somewhat of an idiom to useDefault<UdtType>() w/ ItemName <- footo kind of get named-item initializers instead of relying purely on positional construction. It may be good to have a language feature similar to Rust's{}-style structs as an alternative way of creating UDT values.