-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqiskit_runner.py
More file actions
65 lines (56 loc) · 1.63 KB
/
Copy pathqiskit_runner.py
File metadata and controls
65 lines (56 loc) · 1.63 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
#!/usr/bin/env python3
import sys
import json
try:
from qiskit import Aer, QuantumCircuit, execute
except ImportError:
Aer = None # type: ignore
from qiskit import BasicAer, QuantumCircuit, execute # type: ignore
else:
try:
from qiskit import BasicAer # type: ignore
except ImportError:
BasicAer = None # type: ignore
if len(sys.argv) < 2:
print("Usage: qiskit_runner.py <qasm_file> [shots]", file=sys.stderr)
sys.exit(1)
qasm_file = sys.argv[1]
shots = 1024
if len(sys.argv) >= 3:
try:
shots = int(sys.argv[2])
except ValueError:
print(f"Invalid shots value: {sys.argv[2]}", file=sys.stderr)
sys.exit(1)
try:
qc = QuantumCircuit.from_qasm_file(qasm_file)
except Exception as exc:
print(f"Error loading QASM file: {exc}", file=sys.stderr)
sys.exit(2)
def resolve_backend():
if Aer is not None:
try:
return Aer.get_backend('qasm_simulator')
except Exception:
if BasicAer is not None:
return BasicAer.get_backend('qasm_simulator')
raise
if BasicAer is not None:
return BasicAer.get_backend('qasm_simulator')
raise RuntimeError('No Qiskit simulator backend available')
try:
backend = resolve_backend()
except Exception as exc:
print(f"Backend resolution error: {exc}", file=sys.stderr)
sys.exit(2)
try:
job = execute(qc, backend=backend, shots=shots)
result = job.result()
except Exception as exc:
print(f"Execution error: {exc}", file=sys.stderr)
sys.exit(3)
try:
counts = result.get_counts(qc)
except Exception:
counts = {}
print(json.dumps(counts))