iTranslated by AI
Playing with Qiskit (2)
Purpose
I found the transpile function in Qiskit somewhat confusing, so I wanted to look into it.
Looking at sources like Introduction to Qiskit, you often see code like this:
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator
simulator = QasmSimulator()
circuit = QuantumCircuit(2, 2)
...
compiled_circuit = transpile(circuit, simulator)
job = simulator.run(compiled_circuit, shots=1000)
But I'm curious about what this transpile is actually doing.
Looking up qiskit.compiler.transpile in the API documentation:
Transpile one or more circuits, according to some desired transpilation targets.
From this, I can understand it "transpiles" something, but not much beyond that. So, let's experiment.
From here on, I'll assume basic imports are already done:
from qiskit import QuantumCircuit, Aer, transpile, IBMQ
from qiskit.visualization import plot_gate_map
from qiskit.tools import job_monitor
Various Circuits and Transpilation Results
Circuit for creating a Bell state
I'd like to transpile a circuit that creates
sim = Aer.get_backend('aer_simulator')
qc = QuantumCircuit(2)
qc.h(0)
qc.x(1)
qc.cx(0, 1)
qc.measure_all()
qc = transpile(qc, sim, basis_gates=['u3', 'cx'])
qc.draw()

I got a straightforward result as shown above.
Circuit for performing a SWAP
qc = QuantumCircuit(2)
qc.swap(0, 1)
qc.measure_all()
sim = Aer.get_backend('aer_simulator')
qc = transpile(qc, sim, basis_gates=['u3', 'cx'])
qc.draw()

At first glance, I thought, "What is this?" but it turns out to be the well-known circuit identity
Circuit for executing a Hadamard gate twice
qc = QuantumCircuit(1)
qc.h(0)
qc.h(0)
qc.measure_all()
sim = Aer.get_backend('aer_simulator')
transpiled_qc = transpile(qc, sim)
transpiled_qc.draw('mpl')

The gates have disappeared! This is because the Hadamard gate
I think I'm starting to get a sense of what "transpile" means. Let's continue.
Circuit for executing CX three times
In the case of a simulator
qc = QuantumCircuit(3)
qc.x(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.cx(2, 0)
qc.measure_all()
sim = Aer.get_backend('aer_simulator')
transpiled_qc = transpile(qc, sim, basis_gates=['u3', 'cx'])
transpiled_qc.draw('mpl')

I obtained a "normal" representation that is no different from before. When I execute the measurement with
result = sim.run(transpiled_qc).result()
print(result.get_counts())
it yields {'110': 1024}, meaning
In the case of a real device
The situation is a bit different when using a real device. I'd like to use ibmq_belem this time.[3]
IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q')
backend = provider.get_backend('ibmq_belem')
plot_gate_map(backend, plot_directed=True)

The qubits are connected in this topology. In the quantum circuit I prepared this time, I made sure that one of the CX gates is not between adjacent qubits. Now, what happens when we transpile it?
Looking at the properties, ibmq_belem has Basis gates: CX, ID, RZ, SX, X, so I'll specify these.
qc = QuantumCircuit(3)
qc.x(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.cx(2, 0)
qc.measure_all()
transpiled_qc = transpile(qc, backend, basis_gates=['cx', 'id', 'rz', 'sx', 'x'])
transpiled_qc.draw('mpl')

I obtained a complex-looking circuit that is significantly different from the simulator case.
The part enclosed in the red box corresponds to the "SWAP gate" we saw earlier. Since ibmq_belem requires qubits to be adjacent to execute a CX gate, the qubits are swapped to move
Let's measure it just in case.
job = backend.run(transpiled_qc)
Submit the experimental job. Generally, there is a wait time, so it's queued rather than executed immediately.
...
Job Status: job is queued (2)
...
Job Status: job has successfully run
The job execution is complete.
result = job.result()
plot_histogram(result.get_counts())

Since it's a NISQ device, there is noise, and states that shouldn't theoretically be observed are mixed in, but we can see that the expected
Summary
It turns out that transpilation rearranges the circuit into an appropriate form according to the status of the device executing it. In the case of a simulator, it's ideal and anything goes, so you get something almost identical to the original circuit. On the other hand, with a real device, there are connectivity topology constraints, so the circuit can sometimes become more complex than expected. Also, be aware that as a result, the circuit depth may change.
It was just a simple experiment, but I'm glad I could confirm it properly.
-
More Circuit Identities would be a helpful reference. ↩︎
-
I didn't specify
basis_gates=['u3', 'cx']this time, because if I do, it represents the identity gate usingI , which looks messy, so I intentionally omitted it. ↩︎U_3 -
I chose this device based on the qubit connectivity topology and the queue length at the time of writing this article. ↩︎
Discussion