Ejemplo n.º 1
0
        public override void AssertProb(IQArray <Pauli> bases, IQArray <Qubit> qubits, double probabilityOfZero, string msg, double tol)
        {
            bool   shouldBeDeterministic;
            Result?expectedResult;

            // Is the probability 0?
            if (Math.Abs(probabilityOfZero - 0) < tol)
            {
                shouldBeDeterministic = true;
                expectedResult        = Result.One;
            }
            else if (Math.Abs(probabilityOfZero - 0.5) < tol)
            {
                shouldBeDeterministic = false;
                expectedResult        = null;
            }
            else if (Math.Abs(probabilityOfZero - 1) < tol)
            {
                shouldBeDeterministic = true;
                expectedResult        = Result.Zero;
            }
            else
            {
                throw new ExecutionFailException(msg);
            }

            this.Simulator?.MaybeDisplayDiagnostic(
                new DebugMessage
            {
                Message = $"shouldBeDeterministic = {shouldBeDeterministic}, expectedResult = {expectedResult}",
            });

            if (!bases.TryGetSingleZ(out var idx))
            {
                var aux = this.Simulator !.QubitManager?.Allocate();
                if (aux == null)
                {
                    throw new NullReferenceException("Qubit manager was null.");
                }

                try
                {
                    this.WriteToScratch(bases, qubits, aux);
                    this.AssertProb(
                        new QArray <Pauli>(new[] { Pauli.PauliZ }),
                        new QArray <Qubit>(new[] { aux }),
                        probabilityOfZero,
                        msg,
                        tol);
                    this.WriteToScratch(
                        new QArray <Pauli>(bases.Reverse()),
                        new QArray <Qubit>(qubits.Reverse()),
                        aux);
                }
                finally
                {
                    this.Simulator !.QubitManager?.Release(aux);
                }
            }
            else
            {
                var isDeterministic = this.IsMeasurementDetermined(qubits[idx].Id, out var result);
                if (isDeterministic == shouldBeDeterministic)
                {
                    if (!isDeterministic || expectedResult == result)
                    {
                        return;
                    }

                    throw new ExecutionFailException(msg);
                }
            }
        }