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 55
Expand file tree
/
Copy pathExtensions.cs
More file actions
84 lines (75 loc) · 2.92 KB
/
Extensions.cs
File metadata and controls
84 lines (75 loc) · 2.92 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System;
using System.Collections.Generic;
using Microsoft.Quantum.Simulation.Common;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.QuantumProcessor.Extensions;
namespace Microsoft.Quantum.IQSharp.ExecutionPathTracer
{
/// <summary>
/// Custom ApplyIfElse used by Tracer to overrides the default behaviour and executes both branches
/// of the conditional statement.
/// </summary>
public class TracerApplyIfElse : ApplyIfElseR<Qubit, Qubit>
{
private SimulatorBase Simulator { get; }
/// <summary>
/// Initializes a new instance of the <see cref="TracerApplyIfElse"/> class.
/// </summary>
public TracerApplyIfElse(SimulatorBase m) : base(m)
{
this.Simulator = m;
}
/// <inheritdoc />
public override Func<(Result, (ICallable, Qubit), (ICallable, Qubit)), QVoid> Body => (q) =>
{
(Result measurementResult, (ICallable onZero, Qubit one), (ICallable onOne, Qubit two)) = q;
onZero.Apply(one);
onOne.Apply(two);
return QVoid.Instance;
};
}
/// <summary>
/// Extension methods to be used with and by <see cref="ExecutionPathTracer"/>.
/// </summary>
public static class Extensions
{
/// <summary>
/// Attaches <see cref="ExecutionPathTracer"/> event listeners to the simulator to generate
/// the <see cref="ExecutionPath"/> of the operation performed by the simulator.
/// </summary>
public static T WithExecutionPathTracer<T>(this T sim, ExecutionPathTracer tracer)
where T : SimulatorBase
{
sim.OnOperationStart += tracer.OnOperationStartHandler;
sim.OnOperationEnd += tracer.OnOperationEndHandler;
sim.Register(
typeof(ApplyIfElseR<Qubit, Qubit>),
typeof(TracerApplyIfElse)
);
return sim;
}
/// <summary>
/// Gets the value associated with the specified key and creates a new entry with the <c>defaultVal</c> if
/// the key doesn't exist.
/// </summary>
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultVal)
{
TValue val;
if (!dict.TryGetValue(key, out val))
{
val = defaultVal;
dict.Add(key, val);
}
return val;
}
/// <summary>
/// Gets the value associated with the specified key and creates a new entry of the default type if
/// the key doesn't exist.
/// </summary>
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)
where TValue : new() => dict.GetOrCreate(key, new TValue());
}
}