iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪐

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 \frac{1}{\sqrt{2}}(\ket{00} + \ket{11}). Following qiskit/compiler/transpiler.py#L657, I'll perform a transpilation using U_3 and CX gates.

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 SWAP_{0,1} = CX_{0,1} CX_{1,0} CX_{0,1}[1]. I'll keep this representation in mind as I'll use it later.

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 H is both Hermitian and unitary, meaning H^2 = I.[2]

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 \ket{110} is obtained, which is expected since this is an ideal simulator.

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 q_2 and q_0 couldn't be placed adjacently, and the connectivity topology of ibmq_belem requires qubits to be adjacent to execute a CX gate, the qubits are swapped to move q_0 to the q_1 line and q_1 to the q_0 line. Since the information of q_0 and q_1 has been swapped, at the time of measurement, classical bit c_0 is at the end of the q_1 line, and classical bit c_1 is at the end of the q_0 line.

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 \ket{110} is observed as the most frequent value.

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.

脚注
  1. More Circuit Identities would be a helpful reference. ↩︎

  2. I didn't specify basis_gates=['u3', 'cx'] this time, because if I do, it represents the identity gate I using U_3, which looks messy, so I intentionally omitted it. ↩︎

  3. I chose this device based on the qubit connectivity topology and the queue length at the time of writing this article. ↩︎

GitHubで編集を提案

Discussion