public override void Assert(IQArray <Pauli> bases, IQArray <Qubit> qubits, Result expected, string msg) { Qubit?filter(Pauli p, Qubit q) => p switch { Pauli.PauliI => null, Pauli.PauliZ => q, _ => throw new InvalidOperationException("Assert on bases other than PauliZ not supported") }; // All qubits are considered whose corresponding measurement basis // is PauliZ. Measurement in PauliI basis are ignored, and other // bases will raise an exception. var qubitsToMeasure = bases.Zip(qubits, filter).WhereNotNull(); // A multi-qubit measurement in the PauliZ basis corresponds to // computing the parity of all involved qubits' measurement values. // (see also https://docs.microsoft.com/quantum/concepts/pauli-measurements#multiple-qubit-measurements) // We use Aggregate to successively XOR a qubit's simulation value // to an accumulator value `accu` that is initialized to `false`. var actual = qubitsToMeasure.Aggregate(false, (accu, qubit) => accu ^ GetValue(qubit)); if (actual.ToResult() != expected) { // If the expected value does not correspond to the actual measurement // value, we throw a Q# specific Exception together with the user // defined message `msg`. throw new ExecutionFailException(msg); } } }
public virtual long StartConditionalStatement(IQArray <Result> results1, IQArray <Result> results2) { if (results1 == null) { return(results2 == null ? 1 : 0); } ; if (results1.Count != results2?.Count) { return(0); } ; return(results1.Zip(results2, (r1, r2) => (r1, r2)).Any(pair => pair.r1 != pair.r2) ? 0 : 1); }
/// <summary> /// The implementation of the operation. /// For the Toffoli simulator, the implementation returns the joint parity of the /// states of the measured qubits. /// That is, Result.One is returned if an odd number of the measured qubits are /// in the One state. /// </summary> public Result Measure__Body(IQArray <Pauli> paulis, IQArray <Qubit> targets) { Qubit?f(Pauli p, Qubit q) => p switch { Pauli.PauliI => null, Pauli.PauliZ => q, _ => throw new InvalidOperationException($"The Toffoli simulator can only Measure in the Z basis.") }; if (paulis.Length != targets.Length) { throw new InvalidOperationException($"Both input arrays for Measure (paulis, targets), must be of same size"); } var qubitsToMeasure = paulis.Zip(targets, f).WhereNotNull(); var result = this.GetParity(qubitsToMeasure); return(result.ToResult()); } }