Qiskit Runtime 사용해보기

Qiskit Runtime은 클라우드 기반으로 제공되는 양자 컴퓨팅 서비스를 더욱 효율적으로 사용할 수 있도록 하는 프로그래밍 모델입니다.

Qiskit으로 작성된 프로그램에서 실제 양자 컴퓨터로 작업을 보내고 받을 때에는 꼭 클라우드를 거치게 되는데, 단 한 번의 작업으로 끝나지 않고 여러 번 작업이 오갈 필요가 있는 경우 (매개변수를 계속 변경해야 하는 경우 등) 큐를 다시 기다려야 하는 등의 불편함이 있습니다. 이때 Qiskit Runtime을 통해 작업을 보내게 될 경우 이 과정을 좀 더 단순화할 수 있습니다: Qiskit Runtime overview. 또한 Qiskit Runtime은 양자 하드웨어 가까이에서 실행되기 때문에 고전 컴퓨터와 양자 컴퓨터를 반복해 오고가야 하는 하이브리드 알고리즘의 경우 상대적으로 지연이 줄어들어 속도 향상을 기대할 수 있습니다.

Sampler와 Estimator

Qiskit Runtime에는 미리 정의된 프로그램으로 Sampler와 Estimator가 포함되어 있습니다.

Sampler를 사용할 경우 여러 양자 회로와 다른 파라미터 조합을 한 번에 실행해볼 수 있으며, 추가 옵션을 통해 오류를 완화한 결과를 도출할 수도 있습니다. 실행 결과는 지정한 shots 수로 정규화된 값이며 이는 일반적으로 Runtime을 사용하지 않고 작업을 보냈을 때 받는 값과 유사합니다. Estimator를 사용할 경우 Sampler와 거의 유사하지만 실행 결과가 양자 관측가능량(observable)의 기댓값이 된다는 점에서 차이가 있습니다. Sampler와 Estimator의 실제 활용 코드 예시는 IBM Quantum Programs에서 확인 가능합니다. 이 중에서 Sampler를 사용해 양자 회로를 돌려보도록 하겠습니다.

Qiskit Runtime으로 양자 회로 돌려보기

가장 먼저 Qiskit과 Qiskit Runtime 설치를 해야합니다.

pip install qiskit
pip install qiskit-ibm-runtime

설치가 끝나셨다면 jupyter notebook을 연 다음 IBM Quantum의 본인 토큰을 등록해주세요.

from qiskit_ibm_runtime import QiskitRuntimeService

QiskitRuntimeService.save_account(channel="ibm_quantum", token="MY_IBM_QUANTUM_TOKEN")

토큰 등록이 잘 되었다면 아래 코드를 통해 사용 가능한 프로그램 목록과 백엔드 목록을 확인하실 수 있습니다.

service = QiskitRuntimeService()
service.pprint_programs()
service.backends()

아래는 간단히 돌려볼 양자 얽힘 회로를 작성해봤습니다.

from qiskit import QuantumCircuit

circuit = QuantumCircuit(5)

circuit.h(0)

for i in range(0, 4):
    circuit.cx(i, i + 1)

circuit.measure_all()
circuit.draw('mpl')

5qubits

위의 회로를 qasm_simulator로 측정할 경우 코드는 다음과 같습니다.

from qiskit import Aer, execute

backend = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend, shots = 1024).result()
counts = result.get_counts()

from qiskit.tools.visualization import plot_histogram
plot_histogram(counts)

simulator_result

이제 Qiskit Runtime의 Sampler와 함께 해당 회로를 qasm_simulator로 측정해봅시다. 이때 사용 가능한 백엔드로는 위에서 실행해본 service.backends()으로부터 확인하실 수 있습니다.

from qiskit_ibm_runtime import QiskitRuntimeService

options = {
	'backend_name': 'ibmq_qasm_simulator'
}

runtime_inputs = {
	'circuit_indices': [0],
	'circuits': circuit
}

service = QiskitRuntimeService(
	channel='ibm_quantum'
)

job = service.run(
	program_id='sampler',
	options=options,
	inputs=runtime_inputs,
	instance='ibm-q/open/main'
)

print(job.job_id)
print(job.status())

result = job.result()
plot_histogram(result.get('quasi_dists'))

runtime_simulator_result

위의 Sampler를 이용한 코드 전문(주석 포함)은 IBM Quantum Programs에서 확인할 수 있습니다.

다만 위의 경우는 Sampler를 이용해 실행했을 뿐 오류를 완화한 결과는 아닙니다. 오류 완화 옵션을 추가하고자 한다면 Sampler의 resilience_settings를 설정해주어야 합니다. 해당 옵션을 추가한 코드는 다음과 같습니다.

from qiskit_ibm_runtime import Sampler

options = {
	'backend_name': 'ibmq_qasm_simulator'
}

resilience_settings = {
	"level": 1
}

result = None
with Sampler([circuit], service=service, options=options, resilience_settings=resilience_settings) as sampler:
	result = sampler([circuit])

plot_histogram(result.quasi_dists)

runtime_simulator_result_rem

위의 코드는 Qiskit Tutorials의 Get started with the Sampler primitive를 참고해 작성하였습니다.

References


💬 Any comments and suggestions will be appreciated.

Leave a comment