From 1c213eeecfdfc31785fa6b412e29cb3e569889dc Mon Sep 17 00:00:00 2001 From: Henry551 Date: Mon, 24 Jun 2024 11:27:09 +0800 Subject: [PATCH] add an example pipeline about QPE Signed-off-by: Henry551 --- samples/contrib/QPE_pipeline_example/QPE.py | 58 +++++++++++++++++++ .../contrib/QPE_pipeline_example/README.md | 26 +++++++++ 2 files changed, 84 insertions(+) create mode 100644 samples/contrib/QPE_pipeline_example/QPE.py create mode 100644 samples/contrib/QPE_pipeline_example/README.md diff --git a/samples/contrib/QPE_pipeline_example/QPE.py b/samples/contrib/QPE_pipeline_example/QPE.py new file mode 100644 index 00000000000..2542136bfc4 --- /dev/null +++ b/samples/contrib/QPE_pipeline_example/QPE.py @@ -0,0 +1,58 @@ +from kfp import dsl + +@dsl.component(base_image="python:3.11.7", packages_to_install=['qiskit-aer', 'qiskit', 'pylatexenc', 'ipywidgets', 'matplotlib']) +def qpe(n: int): + from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister, transpile + from qiskit_aer import AerSimulator + from qiskit.visualization import plot_histogram + from math import pi + + count_no = n #the number of count qubits + countreg = QuantumRegister(count_no,'count') + psireg = QuantumRegister(1,'psi') + creg = ClassicalRegister(count_no,'c') + qc = QuantumCircuit(countreg,psireg,creg) + + + for countbit in range(count_no): + qc.h(countbit) + qc.x(psireg) + repeat = 1 + for countbit in range(count_no): + for r in range(repeat): + qc.cp(pi/2,countbit,psireg) + repeat *= 2 + qc.barrier() + + + + for sbit in range(count_no//2): #sbit: for swap qubit + qc.swap(sbit,count_no-sbit-1) + + for hbit in range(0,count_no,1): #hbit: for h-gate qubit + for cbit in range(hbit-1,-1,-1): #cbit: for count qubit + qc.cp(-pi/2**(hbit-cbit), cbit, hbit) + qc.h(hbit) + qc.barrier() + + + qc.measure(range(count_no),range(count_no)) + #display(qc.draw('mpl')) + + sim = AerSimulator() + backend=sim + new_circuit = transpile(qc, backend) + job = backend.run(new_circuit) + result = job.result() + counts = result.get_counts(qc) + print("Total counts for qubit states are:",counts) + plot_histogram(counts) + +@dsl.pipeline +def qpe_pipeline(num: int): + + qpe_task = qpe(n=num) + +from kfp import compiler + +compiler.Compiler().compile(qpe_pipeline, 'QPE-pipeline.yaml') \ No newline at end of file diff --git a/samples/contrib/QPE_pipeline_example/README.md b/samples/contrib/QPE_pipeline_example/README.md new file mode 100644 index 00000000000..2bd2a314871 --- /dev/null +++ b/samples/contrib/QPE_pipeline_example/README.md @@ -0,0 +1,26 @@ +# Quantum Phase Estimation (QPE) Pipeline + +This repository contains a Kubeflow pipeline for running a Quantum Phase Estimation (QPE) algorithm using Qiskit, a quantum computing framework. The pipeline leverages the kfp library to define and compile the pipeline. + +## Requirements +To run this pipeline, you need to have the following installed: + +Python 3.11.7 (base-image) +Kubeflow Pipelines SDK (kfp) + +In pipeline: + +`Qiskit Aer` + +`Qiskit` + +`pylatexenc` + +`ipywidgets` + +`matplotlib` + +## Pipeline Components +qpe Component + +This component runs the Quantum Phase Estimation algorithm. It takes an integer `n` as an input, which specifies the number of counting qubits to use in the algorithm.