Comments (3)
I have verified that adding ResetAll(qubits) at the end of AssertEqualOnBasisVector seems to address the behavior. Not sure whether the absence of the added line is the bug or if some tolerance code is not behaving to spec.
from qsharp-runtime.
Thanks for the detailed issue, @tobiashagge! There are a few points to go over in this and I will get into more detail below, but the summary is that your suggestion of this being an issue with the tolerance in AssertEqualOnBasisVector
is correct. I’ll work on a PR to resolve this.
Regarding your first point in your expected behavior above:
When the theta = .1, an exception, but one that does not claim that the expected value should be "Zero".
This is actually the expected error message, and is consistent with the behavior of our other assert APIs. Most of the different assert operations are built from calls to these two operations:
And
The other asserts use combinations of gate operations, entanglement, and calls to these two functions to enforce the assertion. Some target qubit or auxiliary qubit deviating from the expected state is the trigger for the assertion, and that’s why you see the message they use.
For your second point regarding the difference in behavior between the two angles in your example,
When theta=.001, the program should complete without throwing an exception.
Here is where the problem with tolerance comes in. Because the AssertOperationsEqualInPlace
does not have a tolerance parameter and instead uses a hard-coded value, it should use the least permissive tolerance possible to adequately enforce the assertion. The value used in this case is 1e-5
which is definitely too permissive. Picking the perfect value is tricky given that floating point precision can vary by platform and standard library (see for example C numeric limits and Machine Epsilon). The underlying C++ full state simulator that checks for qubits in the Zero state on release has an epsilon of approx 2e-16 on Windows. Looking through our codebase, there are a few other places where we rely on an epsilon/tolerance, and it seems the best value to standardize behind is 1e-15
since that’s what the Q# implementation of Double
conversions uses:
qsharp-runtime/src/Simulation/QSharpFoundation/Math/Math.qs
Lines 532 to 539 in a860518
Combining this with your suggestion of including a
ResetAll
before the qubits are released will bring the behavior of AssertOperationsEqualInPlace
in line with our other assertion utilities.
On that note of other assertion utilities, if your goal is to have an efficient validation that two unitaries are equivalent for all input states, I would suggest switching to AssertOperationsEqualReferenced instead. As noted in the docs, AssertOperationsEqualInPlace
requires both additional qubits and multiple calls to the provided unitaries, while AssertOperationsEqualReferenced
uses Choi–Jamiołkowski isomorphism to perform the same validation with only one call to the underlying unitaries. Using the examples you posted above, AssertOperationsEqualReferenced
correctly asserts on both inputs and runs faster. This is the same strategy we use in our test cases to validate equivalence of different decompositions of quantum gates, and is quite handy.
from qsharp-runtime.
Hmm… looks like 1e-15
is a reasonable value for the full state simulator, but fails the tests for the stabilizer simulator based on the noise levels. To make these tests still pass, I’ll use the same tolerance used by the other tests:
That’s the value that gets passed for other test operations that call
AssertQubitInStateWithinTolerance
.from qsharp-runtime.
Related Issues (20)
- Dump the Bloch sphere coordinates of a Qubit (from the simulator) HOT 2
- QirArray::buffer: Proper alignment is not guaranteed. HOT 1
- CMake Build Native, NativeSparseSimulator, QIR failed HOT 3
- With LLVM-14 the Leak Sanitizer crashes after completion of `fullstate-simulator-tests`
- DumpMachine output uses different ket symbols depending on the relative phase HOT 4
- Inconsistent output for QuantumSimulator vs SparseSimulator HOT 3
- Trace throws exception on controlled operation HOT 1
- DumpMachine does not produce console output when running from Q# unit tests
- ModPowL may entry a loops when set exponent to "0L" HOT 1
- Ceiling(NaN()) causes an overflow HOT 2
- Simulators behave differently when not resetting qubit array
- TraceSimulator and ResourcesEstimator depth is incorrect with measurements
- Submitting to Azure Quantum hardware targets from localized environments with decimal separators other than `.` can fail
- A wrong display when using DumpMachine()
- Big value cause an overflow of Truncate
- Unable to load DLL 'Microsoft.Quantum.Simulator.Runtime' when running Q# test project
- DumpMachine() raise Unhandled exception
- Endianness of Sparse Simulator DumpMachine is Stateful
- Quantum sample project run crashes with "exited with code 134" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qsharp-runtime.