Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

An issue about the AssertQubitWithinTolerance operation #990

Open
QuXing9 opened this issue Apr 11, 2022 · 3 comments
Open

An issue about the AssertQubitWithinTolerance operation #990

QuXing9 opened this issue Apr 11, 2022 · 3 comments

Comments

@QuXing9
Copy link

QuXing9 commented Apr 11, 2022

Description

Hello! As shown in the below testcase, a qubit is defined, which is named NISLVariable0. When H is applied to NISLVariable0, it has a 50% chance of being 0 and a chance of 50% being 1. But when excepted is set to Zero and tolerance is set to 0.5, the operation AssertQubitWithinTolerance throws an exception. Is this a normal behavior of the operation? And what does the tolerance parameter mean?

Testcase

namespace NISLNameSpace {
	open Microsoft.Quantum.Intrinsic;
	open Microsoft.Quantum.Convert;
	open Microsoft.Quantum.Math;
	open Microsoft.Quantum.Diagnostics;


	operation NISLOperation () : Unit {
		mutable expected = Zero;
		use NISLVariable0 = Qubit();
		H(NISLVariable0);
		mutable tolerance = 0.5;
		AssertQubitWithinTolerance(expected, NISLVariable0, tolerance);
        	Reset(NISLVariable0);
	}

	@EntryPoint()
	operation main() : Unit {
		mutable tmp = NISLOperation();
	}
}

Actual Behavior

Unhandled exception. Microsoft.Quantum.Simulation.Core.ExecutionFailException: Qubit in invalid state. Expecting: Zero with tolerance 0.5
        Expected:       1
        Actual: 0.4999999999999999

Expected Behavior

No exception

Environment

Operating system: Windows 10

dotnet version: 3.1.414

QDK: 0.21.2111177148

Command

dotnet run

@msoeken msoeken self-assigned this Apr 11, 2022
@msoeken msoeken transferred this issue from microsoft/QuantumLibraries Apr 12, 2022
@msoeken
Copy link
Member

msoeken commented Apr 12, 2022

@QuXing9: I am transferring this issue, since AssertQubitWithinTolerance is implemented in this repository. It seems that the problem arises from this line:

where a > comparison is used, which might be intended. Therefore, you would need to reduce your tolerance to a value below 0.5, e.g., 0.49.

@msoeken msoeken removed their assignment Apr 12, 2022
@msoeken msoeken removed the tracking label Apr 12, 2022
@QuXing9
Copy link
Author

QuXing9 commented Apr 13, 2022

Thanks for the reply! I understand the meaning of the tolerance parameter.
Meanwhile, there is another question that confused me.
from this line:

var ensemblePr = this.Simulator.JointEnsembleProbability((uint)paulis.Length, paulis.ToArray(), qubits.GetIds());

ensemblePr will always return 0.500……1. Does this mean that when H operation is applied to a qubit, it returns Zero with a probability of 0.499……9 and One with a probability of 0.500……1 ? Why not just return 0.5?

@QuXing9
Copy link
Author

QuXing9 commented Apr 13, 2022

By the way, there is a small suggestion.

Since the value of tolerance ranges from 0.0 to 1.0. If it is less than 0.0, the AssertQubitWithinTolerance will throw an exception always. If it is greater than 1.0. the AssertQubitWithinTolerance will execute correctly. Therefore, is it possible to add judgment or assertion on tolerance value at Line 28 of the AssertMeasurementProbability ? In this way, it will avoid the execution of redundant code and improve the efficiency of program execution.

var (paulis, qubits, result, expectedPr, msg, tol) = _args;

Forgive me if I'm wrong.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants