-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIntrinsics.cs
More file actions
111 lines (96 loc) · 3.39 KB
/
Intrinsics.cs
File metadata and controls
111 lines (96 loc) · 3.39 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Linq;
using Microsoft.Quantum.Simulation.Core;
using Intrinsic = Microsoft.Quantum.Intrinsic;
namespace QSharpCommunity.Simulators.OpenQasmExporter.Circuits
{
public class Measure : Intrinsic.Measure
{
public Measure(IOperationFactory m)
: base(m) { }
public override Func<(IQArray<Pauli>, IQArray<Qubit>), Result> __Body__ =>
args =>
{
var (bases, qubits) = args;
foreach (var q in qubits)
{
Console.WriteLine($"measure q[{q.Id}] -> c[{q.Id}];");
}
return Result.Zero;
};
}
// It's necessary to override this operation, so it doesn't end up in the OpenQASM output
public class Message : Intrinsic.Message
{
public Message(IOperationFactory m)
: base(m) { }
public override Func<string, QVoid> __Body__ =>
msg =>
{
// Ignore
return QVoid.Instance;
};
}
// It's necessary to override this operation, so it doesn't end up in the OpenQASM output
public class Reset : Intrinsic.Reset
{
public Reset(IOperationFactory m)
: base(m) { }
public override Func<Qubit, QVoid> __Body__ =>
qubit =>
{
return QVoid.Instance;
};
}
// It's necessary to override this operation, so it doesn't end up in the OpenQASM output
public class ResetAll : Intrinsic.ResetAll
{
public ResetAll(IOperationFactory m)
: base(m) { }
public override Func<IQArray<Qubit>, QVoid> __Body__ =>
args =>
{
return QVoid.Instance;
};
}
public class SingleQubitOp<T> : Intrinsic.I, ICallable
{
string ICallable.Name => typeof(T).Name;
public override Func<Qubit, QVoid> __Body__ =>
qubit =>
{
var opName = ((ICallable)this).Name.ToLower();
Console.WriteLine($"{opName} q[{qubit.Id}];");
return QVoid.Instance;
};
public override Func<(IQArray<Qubit>, Qubit), QVoid> __ControlledBody__ =>
args =>
{
var (controls, qubit) = args;
var controlCount = controls.Count;
var controlPrefix = string.Concat(Enumerable.Repeat("c", controlCount));
var opName = ((ICallable)this).Name.ToLower();
Console.WriteLine($"{controlPrefix}{opName} {string.Join(",", controls.Select(c => $"q[{c.Id}]"))},q[{qubit.Id}];");
return QVoid.Instance;
};
public override Func<Qubit, QVoid> __AdjointBody__ =>
qubit =>
{
var opName = ((ICallable)this).Name;
switch (opName)
{
case nameof(Intrinsic.T):
case nameof(Intrinsic.S):
opName = opName.ToLower() +"dg";
break;
default:
opName = opName.ToLower();
break;
}
Console.WriteLine($"{opName} q[{qubit.Id}];");
return QVoid.Instance;
};
public SingleQubitOp(IOperationFactory m)
: base(m) {}
}
}