-
Notifications
You must be signed in to change notification settings - Fork 5
/
QiskitQasmRunner.cs
86 lines (79 loc) · 3.1 KB
/
QiskitQasmRunner.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace progettoreti
{
/// <summary>
/// Quick version to tranfer to python in a real linux environment, because Qiskit doesn't run on the windows python
/// </summary>
internal static class QiskitExecutor
{
/// <summary>
/// Use the linux subsystem, to run in a real linux and run the python code to execute the Qasm
/// </summary>
public static string RunQasm(StringBuilder qasm, int qbits, string key, string backend, int shots)
{
try
{
var input = "input.txt";
var output = "output.txt";
//Change to unix compatible file format
File.WriteAllText(input, qasm.ToString().Replace("\r\n", "\n"), Encoding.ASCII);
//Run python3 with the interface
var python = "python3";
var arguments = $"QiskitInterface.py {key} {backend} {shots}";
//HACK: fix because currently qiskit currently can't run directly within windows,
// so wrap it in linux subsystem of bash
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
python = "bash.exe";
arguments = $"-c \"python3 {arguments}\"";
}
var processStart = new ProcessStartInfo()
{
FileName = python,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
var process = new Process()
{
StartInfo = processStart,
EnableRaisingEvents = true
};
process.OutputDataReceived += (p, o) => Console.WriteLine(o.Data);
process.ErrorDataReceived += (p, o) => Console.Error.WriteLine(o.Data);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
if (File.Exists(output))
{
var result = File.ReadAllText(output);
if (result.Contains("'labels':"))
{
result = result.Substring(result.IndexOf("'labels': ['") + 12, qbits);
}
return result;
}
else
{
Console.WriteLine("Missing output in outputfile");
return "";
}
}
catch (Exception e)
{
Console.WriteLine($"Starting QiskitInterface.py failed because of: {e.Message}");
return "";
}
}
}
}