7
$\begingroup$

I can find many qasm examples. How can I run them on different IBMQ devices?

$\endgroup$

3 Answers 3

9
$\begingroup$

This is a example for loading QASM, executing and displaying the result.

from qiskit import QuantumCircuit, Aer, execute

qasm_str = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
measure q -> c;
"""

# From str.
qc = QuantumCircuit.from_qasm_str(qasm_str)
# If you want to read from file, use instead
# qc = QuantumCircuit.from_qasm_file("/path/to/file.qasm")

# You can choose other backend also.
backend = Aer.get_backend("qasm_simulator")

# Execute the circuit and show the result.
job = execute(qc, backend)
result = job.result()
print(result.get_counts())
$\endgroup$
7
$\begingroup$

The easiest way is to use the QuantumCircuit methods QuantumCircuit.from_qasm_file() or QuantumCircuit.from_qasm_str() depending on if your loading the QASM from a file or Python string, respectively.

$\endgroup$
2
  • 1
    $\begingroup$ Thanks. You means use QuantumCircuit methods in Jupyter notebook? $\endgroup$
    – peachnuts
    Commented Jan 29, 2020 at 20:12
  • $\begingroup$ You can use them anywhere to construct a circuit from QASM code. $\endgroup$ Commented Jan 29, 2020 at 20:12
-3
$\begingroup$
from qiskit import *
simulator = Aer.get_backend('qasm_simulator')
$\endgroup$
3
  • 2
    $\begingroup$ Please provide some explanation, i.e. how the code solve a problem in the question. $\endgroup$ Commented Feb 5, 2020 at 22:51
  • $\begingroup$ Shall I link GitHub repository inorder to do tat? $\endgroup$ Commented Feb 6, 2020 at 6:15
  • 2
    $\begingroup$ Reddy: It is also possible, but please add some comments, like where to put your code etc. Please take some inspiration from another answers - look for tag Qiskit. $\endgroup$ Commented Feb 6, 2020 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.