Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add(pipeline/samples/contrib/QPE_pipeline_example): a pipeline example about QPE #10947

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/QPE.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: QPE_example

on:
workflow_dispatch:

jobs:
samples:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Create KFP cluster
uses: ./.github/actions/kfp-cluster

- name: Forward API port
run: ./scripts/deploy/github/forward-port.sh "kubeflow" "ml-pipeline" 8888 8888

- name: Grant execute permissions to scripts
shell: bash
run: chmod +x ./samples/contrib/QPE_pipeline_example/QPE.sh

- name: Install dos2unix
run: sudo apt-get update && sudo apt-get install dos2unix

- name: Run Samples Tests
run: dos2unix ./samples/contrib/QPE_pipeline_example/QPE.sh && ./samples/contrib/QPE_pipeline_example/QPE.sh
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: QPE_test

on:
workflow_dispatch:

jobs:
samples:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Create KFP cluster
uses: ./.github/actions/kfp-cluster

- name: Forward API port
run: ./scripts/deploy/github/forward-port.sh "kubeflow" "ml-pipeline" 8888 8888

- name: Grant execute permissions to scripts
shell: bash
run: chmod +x ./samples/contrib/QPE_pipeline_example/QPE.sh

- name: Install dos2unix
run: sudo apt-get update && sudo apt-get install dos2unix

- name: Run Samples Tests
run: dos2unix ./samples/contrib/QPE_pipeline_example/QPE.sh && ./samples/contrib/QPE_pipeline_example/QPE.sh
58 changes: 58 additions & 0 deletions samples/contrib/QPE_pipeline_example/QPE.py
Original file line number Diff line number Diff line change
@@ -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')
28 changes: 28 additions & 0 deletions samples/contrib/QPE_pipeline_example/QPE.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
#
# Copyright 2021 The Kubeflow Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

pushd ./backend/src/v2/test

python3 -m pip install --upgrade pip
python3 -m pip install -r ./requirements-sample-test.txt

popd

# The -u flag makes python output unbuffered, so that we can see real time log.
# Reference: https://stackoverflow.com/a/107717
python3 -u ./samples/contrib/QPE_pipeline_example/QPE.py
26 changes: 26 additions & 0 deletions samples/contrib/QPE_pipeline_example/README.md
Original file line number Diff line number Diff line change
@@ -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.
49 changes: 49 additions & 0 deletions samples/contrib/QPE_pipeline_example/sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import unittest
from dataclasses import dataclass
from pprint import pprint
from typing import List

import kfp
from kfp.dsl.graph_component import GraphComponent
import QPE

_MINUTE = 60 # seconds
_DEFAULT_TIMEOUT = 5 * _MINUTE


@dataclass
class TestCase:
pipeline_func: GraphComponent
timeout: int = _DEFAULT_TIMEOUT


class SampleTest(unittest.TestCase):
_kfp_host_and_port = os.getenv('KFP_API_HOST_AND_PORT', 'http://localhost:8888')
_kfp_ui_and_port = os.getenv('KFP_UI_HOST_AND_PORT', 'http://localhost:8080')
_client = kfp.Client(host=_kfp_host_and_port, ui_host=_kfp_ui_and_port)

def test(self):
# 只測試 hello_world 範例
test_case = TestCase(pipeline_func=QPE.qpe_pipeline)

# 直接執行測試案例
self.run_test_case(test_case.pipeline_func, test_case.timeout)

def run_test_case(self, pipeline_func: GraphComponent, timeout: int):
# 執行指定的管道函數,並等待完成
with self.subTest(pipeline=pipeline_func, msg=pipeline_func.name):
run_result = self._client.create_run_from_pipeline_func(pipeline_func=pipeline_func)
run_response = run_result.wait_for_run_completion(timeout)

# 打印運行的詳細信息
pprint(run_response.run_details)
print("Run details page URL:")
print(f"{self._kfp_ui_and_port}/#/runs/details/{run_response.run_id}")

# 檢查運行狀態是否成功
self.assertEqual(run_response.state, "SUCCEEDED")


if __name__ == '__main__':
unittest.main()