Transpile using REST API
This documentation utilizes the Python requests
module to demonstrate the Qiskit transpiler service API. However, this workflow can be executed using any language or framework that supports working with REST APIs. Refer to the API reference documentation for details.
The process of rewriting a given input circuit to match the topology of a specific quantum device, and optimizing the circuit instructions for execution on noisy quantum QPUs, is known as transpilation.
You have two transpilation options:
-
Transpile locally via Qiskit before generating the QASM string.
Transpilation is necessary(opens in a new tab) prior to submitting a circuit to IBM® QPUs.
The steps in this topic describe how to transpile a given QASM circuit and obtain results using the Cloud Transpiler REST API, and include suggestions on how to submit the transpiled quantum circuit to IBM compute resources.
Query the Qiskit transpiler service
Query the Qiskit transpiler service API(opens in a new tab) and provide your QASM string as input. See more details in the API reference documentation.
There are two ways to invoke the Qiskit transpiler service using REST API: with regular transpilation, and with AI-enhanced transpilation. The following demonstrates both ways to invoke the Qiskit transpiler service API.
This experimental service is only available to IBM Quantum™ Premium Plan users.
backend='ibm_brisbane' #choose your IBM Backend
headers = {
"accept": "application/json",
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
body= {
"qasm_circuits": qasm_string,
}
params = {
"backend": backend,
"optimization_level": 1,
"ai": "false", # "true", "false" and "auto"
}
resp = requests.post(
"https://cloud-transpiler.quantum.ibm.com/transpile",
headers=headers,
json=body,
params=params,
)
backend='ibm_brisbane' #choose your IBM Backend
headers = {
"accept": "application/json",
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
body= {
"qasm_circuits": qasm_string,
}
params = {
"backend": backend,
"optimization_level": 1,
"ai": "true", # "true", "false" and "auto"
"coupling_map": [],
"qiskit_transpile_options": [],
"ai_layout_mode": 'OPTIMIZE', # 'KEEP', 'OPTIMIZE', 'IMPROVE'
}
resp = requests.post(
"https://cloud-transpiler.quantum.ibm.com/transpile",
headers=headers,
json=body,
params=params,
)
Since there might be cases where it’s more effective not to use AI-enhanced passes, you can set the ai
parameter to ai="auto"
to enable the QPU to decide automatically whether to apply the standard Qiskit heuristic passes or the new AI-powered passes, based on the particulars of your circuit.
Request results based on the task_id
Request the transpilation service results using the task_id
.
task_id=resp.json()['task_id']
res = requests.get(url=f"https://cloud-transpiler.quantum.ibm.com/transpile/{task_id}", headers=headers)
if res.json().get("state") == "SUCCESS":
resulting_qasm=res.json().get("result")[0].get("qasm")
print(resulting_qasm)
Output
OPENQASM 3.0; include "stdgates.inc"; gate rzx_140040114706704(_gate_p_0) _gate_q_0, _gate_q_1 { h _gate_q_1; cx _gate_q_0, _gate_q_1; rz(pi/4) _gate_q_1; cx _gate_q_0, _gate_q_1; h _gate_q_1; } gate rzx_140040024191376(_gate_p_0) _gate_q_0, _gate_q_1 { h _gate_q_1; cx _gate_q_0, _gate_q_1; rz(-pi/4) _gate_q_1; cx _gate_q_0, _gate_q_1; h _gate_q_1; } gate ecr _gate_q_0, _gate_q_1 { rzx_140040114706704(pi/4) _gate_q_0, _gate_q_1; x _gate_q_0; rzx_140040024191376(-pi/4) _gate_q_0, _gate_q_1; } bit[2] c; rz(-pi) $0; sx $0; rz(pi/2) $0; rz(-pi/2) $1; sx $1; rz(-pi) $1; ecr $1, $0; rz(-pi/2) $0; sx $0; rz(pi/2) $0; rz(pi/2) $1; sx $1; rz(pi/2) $1; barrier $0, $1; c[0] = measure $0; c[1] = measure $1;
Next steps
- Review detailed Sampler and Estimator primitives examples using REST API.
- Read Migrate to V2 primitives.
- Practice with primitives by working through the Cost function lesson(opens in a new tab) in IBM Quantum Learning.
- Learn how to transpile locally in the Transpile section.