This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 940
Expand file tree
/
Copy pathFaultyMeasurementsSimulator.cs
More file actions
65 lines (56 loc) · 2.44 KB
/
FaultyMeasurementsSimulator.cs
File metadata and controls
65 lines (56 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using Microsoft.Quantum.Simulation.Simulators;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic;
namespace Microsoft.Quantum.Samples.SimulatorWithOverrides
{
/// <summary>
/// A simulator which extends QuantumSimulator and redefines measurement operation
/// to introduce a bit-flip error happening before measurement with certain probability.
/// </summary>
public class FaultyMeasurementsSimulator : QuantumSimulator
{
/// <summary>
/// The overriding definition for operation M
/// </summary>
public class M : QSimM
{
/// <summary>
/// The probability with which the error will be introduced before measurement.
/// </summary>
const double flipProbability = 0.1;
/// <summary>
/// Random number generator used to decide when to introduce the error.
/// </summary>
private static readonly System.Random rnd = new System.Random();
public M(FaultyMeasurementsSimulator m) : base(m) { }
/// <summary>
/// The actual definition of what the new operation does.
/// </summary>
public override Func<Qubit, Result> __Body__
{
get
{
// Get the original M operation to call it and process the results
Func<Qubit, Result> originalMeasurementOperation = base.__Body__;
// Get the X gate operation (used to introduce the error)
IUnitary<Qubit> gateX = this.__Factory__.Get<IUnitary<Qubit>>(typeof(X));
// The body of the operation is a lambda
return (qubit =>
{
// Introduce the X error with certain probability
if (rnd.NextDouble() < flipProbability)
{
gateX.Apply(qubit);
}
// Call the original (perfect) M operation to get final measurement results.
// Q# type Result which denotes measurement results maps to C# type with the same name
return originalMeasurementOperation(qubit);
});
}
}
}
}
}