ข้ามไปยังเนื้อหาหลัก

Singularity Machine Learning - Classification: A Qiskit Function by Multiverse Computing

Note
  • Qiskit Functions เป็นฟีเจอร์ทดลองที่ให้บริการเฉพาะผู้ใช้แผน IBM Quantum® Premium Plan, Flex Plan และ On-Prem (ผ่าน IBM Quantum Platform API) เท่านั้น อยู่ในสถานะ preview release และอาจมีการเปลี่ยนแปลงได้

ภาพรวม

ด้วยฟังก์ชัน "Singularity Machine Learning - Classification" คุณสามารถแก้ปัญหา machine learning ในโลกจริงบน quantum hardware ได้โดยไม่ต้องมีความเชี่ยวชาญด้าน quantum ฟังก์ชัน Application นี้อิงจาก ensemble methods และเป็น hybrid classifier ที่ใช้ประโยชน์จากวิธีแบบคลาสสิก เช่น boosting, bagging และ stacking สำหรับการฝึก ensemble เบื้องต้น จากนั้นจึงนำ quantum algorithms อย่าง variational quantum eigensolver (VQE) และ quantum approximate optimization algorithm (QAOA) มาใช้เพื่อเพิ่มความหลากหลาย ความสามารถในการ generalization และความซับซ้อนโดยรวมของ ensemble ที่ฝึกแล้ว

ต่างจาก quantum machine learning อื่น ๆ ฟังก์ชันนี้สามารถจัดการชุดข้อมูลขนาดใหญ่ที่มีตัวอย่างและ feature หลายล้านรายการได้ โดยไม่ถูกจำกัดด้วยจำนวน Qubit ใน QPU เป้าหมาย จำนวน Qubit จะกำหนดแค่ขนาดของ ensemble ที่สามารถฝึกได้เท่านั้น ฟังก์ชันนี้ยังมีความยืดหยุ่นสูง และสามารถใช้แก้ปัญหา classification ในหลากหลายโดเมน ทั้งด้านการเงิน สาธารณสุข และความปลอดภัยทางไซเบอร์ มันสามารถทำความแม่นยำสูงอย่างสม่ำเสมอบนปัญหาที่ท้าทายสำหรับคอมพิวเตอร์แบบคลาสสิก ซึ่งมีข้อมูลมิติสูง มีสัญญาณรบกวน และมีความไม่สมดุลของคลาส How it works เหมาะสำหรับ:

  1. วิศวกรและนักวิทยาศาสตร์ข้อมูลในบริษัทที่ต้องการยกระดับผลิตภัณฑ์และบริการด้วยการนำ quantum machine learning มาผสานรวม
  2. นักวิจัยในห้องปฏิบัติการวิจัย quantum ที่กำลังสำรวจการประยุกต์ใช้ quantum machine learning และต้องการใช้ quantum computing สำหรับงาน classification และ
  3. นักศึกษาและอาจารย์ในสถาบันการศึกษาในรายวิชา machine learning ที่ต้องการแสดงให้เห็นถึงข้อได้เปรียบของ quantum computing

ตัวอย่างต่อไปนี้แสดงฟังก์ชันต่าง ๆ ของมัน ได้แก่ create, list, fit และ predict และแสดงวิธีใช้งานกับปัญหาสังเคราะห์ที่ประกอบด้วยครึ่งวงกลมสองอันเชื่อมต่อกัน ซึ่งเป็นปัญหาที่ท้าทายอย่างมากเนื่องจาก decision boundary ที่ไม่เป็นเส้นตรง

คำอธิบายฟังก์ชัน

Qiskit Function นี้ช่วยให้ผู้ใช้สามารถแก้ปัญหา binary classification โดยใช้ quantum-enhanced ensemble classifier ของ Singularity เบื้องหลัง มันใช้แนวทาง hybrid ในการฝึก ensemble ของ classifier แบบคลาสสิกบนชุดข้อมูลที่มี label แล้วปรับแต่งให้มีความหลากหลายและ generalization สูงสุดโดยใช้ Quantum Approximate Optimization Algorithm (QAOA) บน IBM® QPUs ผ่านอินเตอร์เฟซที่ใช้งานง่าย ผู้ใช้สามารถกำหนดค่า classifier ตามความต้องการ ฝึกมันบนชุดข้อมูลที่เลือก และใช้มันทำนายผลบนชุดข้อมูลที่ไม่เคยเห็นมาก่อน

เพื่อแก้ปัญหา classification ทั่วไป:

  1. ประมวลผลชุดข้อมูลล่วงหน้า และแบ่งออกเป็นชุดฝึกและชุดทดสอบ ถ้าต้องการ สามารถแบ่งชุดฝึกออกเป็นชุดฝึกและชุด validation เพิ่มเติมได้ ทำได้โดยใช้ scikit-learn
  2. ถ้าชุดฝึกไม่สมดุล คุณสามารถทำการ resample เพื่อปรับสมดุลของคลาสได้โดยใช้ imbalanced-learn
  3. อัปโหลดชุดฝึก ชุด validation และชุดทดสอบแยกกันไปยัง storage ของฟังก์ชัน โดยใช้เมธอด file_upload ของ catalog และส่ง path ที่เกี่ยวข้องในแต่ละครั้ง
  4. เริ่มต้น quantum classifier โดยใช้ action create ของฟังก์ชัน ซึ่งรับ hyperparameter ต่าง ๆ เช่น จำนวนและประเภทของ learner, regularization (ค่า lambda) และตัวเลือกการ optimization รวมถึงจำนวน layer, ประเภทของ classical optimizer, quantum Backend และอื่น ๆ
  5. ฝึก quantum classifier บนชุดฝึกโดยใช้ action fit ของฟังก์ชัน โดยส่งชุดฝึกที่มี label และชุด validation (ถ้ามี)
  6. ทำนายผลบนชุดทดสอบที่ไม่เคยเห็นมาก่อนโดยใช้ action predict ของฟังก์ชัน

แนวทางแบบ action-based

ฟังก์ชันนี้ใช้แนวทางแบบ action-based ลองนึกภาพว่ามันเป็นสภาพแวดล้อมเสมือนที่คุณใช้ action เพื่อทำงานหรือเปลี่ยนสถานะของมัน ปัจจุบันมี action ดังต่อไปนี้: list, create, delete, fit, predict, fit_predict และ create_fit_predict ตัวอย่างต่อไปนี้แสดงการใช้งาน action create_fit_predict

# Added by doQumentation — required packages for this notebook
!pip install -q numpy qiskit-ibm-catalog scikit-learn
# Import QiskitFunctionsCatalog to load the
# "Singularity Machine Learning - Classification" function by Multiverse Computing
from qiskit_ibm_catalog import QiskitFunctionsCatalog

# Import the make_moons and the train_test_split functions from scikit-learn
# to create a synthetic dataset and split it into training and test datasets
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split

# authentication
# If you have not previously saved your credentials, follow instructions at
# /docs/guides/functions
# to authenticate with your API key.
catalog = QiskitFunctionsCatalog(channel="ibm_quantum_platform")

# load "Singularity Machine Learning - Classification" function by Multiverse Computing
singularity = catalog.load("multiverse/singularity")

# generate the synthetic dataset
X, y = make_moons(n_samples=1000)

# split the data into training and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

job = singularity.run(
action="create_fit_predict",
num_learners=10,
regularization=0.01,
optimizer_options={"simulator": True},
X_train=X_train,
y_train=y_train,
X_test=X_test,
options={"save": False},
)

# get job status and result
status = job.status()
result = job.result()

print("Job status: ", status)
print("Action result status: ", result["status"])
print("Action result message: ", result["message"])
print("Predictions (first five results): ", result["data"]["predictions"][:5])
print(
"Probabilities (first five results): ",
result["data"]["probabilities"][:5],
)
print("Usage metadata: ", result["metadata"]["resource_usage"])
Job status:  QUEUED
Action result status: ok
Action result message: Classifier created, fitted, and predicted.
Predictions (first five results): [1, 0, 0, 1, 0]
Probabilities (first five results): [[0.16849563539001172, 0.8315043646099888], [0.8726393386620336, 0.12736066133796647], [0.795344837290717, 0.20465516270928288], [0.36822585748882725, 0.6317741425111725], [0.6656662698604361, 0.3343337301395641]]
Usage metadata: {'RUNNING: MAPPING': {'CPU_TIME': 7.945035696029663}, 'RUNNING: WAITING_QPU': {'CPU_TIME': 82.41029238700867}, 'RUNNING: POST_PROCESSING': {'CPU_TIME': 77.3459484577179}, 'RUNNING: EXECUTING_QPU': {'QPU_TIME': 71.27004957199097}}

1. List

action list จะดึงข้อมูล classifier ทั้งหมดที่จัดเก็บในรูปแบบ *.pkl.tar จาก shared data directory คุณสามารถเข้าถึงเนื้อหาของ directory นี้ได้โดยใช้เมธอด catalog.files() ด้วย โดยทั่วไป action list จะค้นหาไฟล์ที่มีนามสกุล *.pkl.tar ใน shared data directory และส่งคืนผลลัพธ์ในรูปแบบ list

Inputs

NameTypeDescriptionRequired
actionstrชื่อของ action จาก create, list, fit, predict, fit_predict, create_fit_predict และ deleteYes

Usage

job = singularity.run(action="list")

2. Create

action create จะสร้าง classifier ตามประเภท quantum_classifier ที่ระบุโดยใช้พารามิเตอร์ที่ให้มา และบันทึกไว้ใน shared data directory

หมายเหตุ

ขณะนี้ฟังก์ชันรองรับเฉพาะ QuantumEnhancedEnsembleClassifier เท่านั้น

Inputs

NameTypeDescriptionRequiredDefault
actionstrชื่อของ action จาก create, list, fit, predict, fit_predict, create_fit_predict และ deleteYes-
namestrชื่อของ quantum classifier เช่น spam_classifierYes-
instancestrIBM instanceYes-
backend_namestrIBM compute resource ค่าเริ่มต้นคือ None ซึ่งหมายถึงจะใช้ Backend ที่มี pending jobs น้อยที่สุดNoNone
quantum_classifierstrประเภทของ quantum classifier คือ QuantumEnhancedEnsembleClassifierNoQuantumEnhancedEnsembleClassifier
num_learnersintegerจำนวน learner ใน ensembleNo10
learners_typeslistประเภทของ learner ที่รองรับได้แก่: DecisionTreeClassifier, GaussianNB, KNeighborsClassifier, MLPClassifier และ LogisticRegression รายละเอียดเพิ่มเติมของแต่ละตัวสามารถดูได้ที่ scikit-learn documentationNo[DecisionTreeClassifier]
learners_proportionslistสัดส่วนของ learner แต่ละประเภทใน ensembleNo[1.0]
learners_optionslistตัวเลือกสำหรับ learner แต่ละประเภทใน ensemble สำหรับรายการตัวเลือกทั้งหมดที่สอดคล้องกับประเภท learner ที่เลือก ให้ดู scikit-learn documentationNo[{"max_depth": 3, "splitter": "random", "class_weight": None}]
regularization_typestr or listประเภทของ regularization ที่จะใช้: onsite หรือ alpha onsite ควบคุม onsite term โดยค่าที่สูงกว่าจะทำให้ ensemble เบาบางมากขึ้น alpha ควบคุมการแลกเปลี่ยนระหว่าง interaction และ onsite term โดยค่าที่ต่ำกว่าจะทำให้ ensemble เบาบางมากขึ้น ถ้าระบุเป็น list จะฝึก model สำหรับแต่ละประเภทและเลือกตัวที่ดีที่สุดNoonsite
regularizationstr or float or listค่า regularization อยู่ระหว่าง 0 ถึง +inf ถ้า regularization_type เป็น onsite อยู่ระหว่าง 0 ถึง 1 ถ้า regularization_type เป็น alpha ถ้าตั้งเป็น auto จะใช้ auto-regularization — หา regularization parameter ที่เหมาะสมที่สุดด้วย binary search โดยใช้อัตราส่วนที่ต้องการของ classifier ที่เลือกต่อ classifier ทั้งหมด (regularization_desired_ratio) และขอบบนของ regularization parameter (regularization_upper_bound) ถ้าระบุเป็น list จะฝึก model สำหรับแต่ละค่าและเลือกตัวที่ดีที่สุดNo0.01
regularization_desired_ratiofloat or listอัตราส่วนที่ต้องการของ classifier ที่เลือกต่อ classifier ทั้งหมดสำหรับ auto-regularization ถ้าระบุเป็น list จะฝึก model สำหรับแต่ละอัตราส่วนและเลือกตัวที่ดีที่สุดNo0.75
regularization_upper_boundfloat or listขอบบนของ regularization parameter เมื่อใช้ auto-regularization ถ้าระบุเป็น list จะฝึก model สำหรับแต่ละขอบบนและเลือกตัวที่ดีที่สุดNo200
weight_update_methodstrวิธีอัปเดต sample weights จาก logarithmic และ quadraticNologarithmic
sample_scalingbooleanว่าจะใช้ sample scaling หรือไม่NoFalse
prediction_scalingfloatscaling factor สำหรับการทำนายNoNone
optimizer_optionsdictionaryตัวเลือก QAOA optimizer รายการตัวเลือกทั้งหมดแสดงอยู่ในเอกสารนี้ด้านล่างNo...
votingstrใช้ majority voting (hard) หรือค่าเฉลี่ยของ probability (soft) สำหรับรวบรวมการทำนาย/probability ของ learnerNohard
prob_thresholdfloatprobability threshold ที่เหมาะสมที่สุดNo0.5
random_stateintegerควบคุมความสุ่มเพื่อให้ผลลัพธ์ซ้ำได้NoNone
  • นอกจากนี้ optimizer_options มีรายการดังต่อไปนี้:
NameTypeDescriptionRequiredDefault
num_solutionsintegerจำนวน solutionNo1024
repsintegerจำนวนรอบNo4
sparsifyfloatsparsification thresholdNo0.001
thetafloatค่าเริ่มต้นของ theta ซึ่งเป็น variational parameter ของ QAOANoNone
simulatorbooleanว่าจะใช้ simulator หรือ QPUNoFalse
classical_optimizerstrชื่อของ classical optimizer สำหรับ QAOA solver ทั้งหมดที่ SciPy นำเสนอตามที่ระบุที่นี่ สามารถใช้ได้ คุณจะต้องตั้งค่า classical_optimizer_options ตามนั้นNoCOBYLA
classical_optimizer_optionsdictionaryตัวเลือก classical optimizer สำหรับรายการตัวเลือกทั้งหมด ดู SciPy documentationNo{"maxiter": 60}
optimization_levelintegerความลึกของ QAOA CircuitNo3
num_transpiler_runsintegerจำนวนรอบที่รัน TranspilerNo30
pass_manager_optionsdictionaryตัวเลือกสำหรับการสร้าง preset pass managerNo{"approximation_degree": 1.0}
estimator_optionsdictionaryตัวเลือก Estimator สำหรับรายการตัวเลือกทั้งหมด ดู Qiskit Runtime Client documentationNoNone
sampler_optionsdictionaryตัวเลือก Sampler สำหรับรายการตัวเลือกทั้งหมด ดู Qiskit Runtime Client documentationNoNone
  • estimator_options เริ่มต้นคือ:
NameTypeValue
default_shotsinteger1024
resilience_levelinteger2
twirlingdictionary{"enable_gates": True}
dynamical_decouplingdictionary{"enable": True}
resilience_optionsdictionary{"zne_mitigation": False, "zne": {"amplifier": "pea", "noise_factors": [1.0, 1.3, 1.6], "extrapolator": ["linear", "polynomial_degree_2", "exponential"],}}
  • sampler_options เริ่มต้นคือ:
NameTypeValue
default_shotsinteger1024
resilience_levelinteger1
twirlingdictionary{"enable_gates": True}
dynamical_decouplingdictionary{"enable": True}

Usage

job = singularity.run(
action="create",
name="classifier_name", # specify your custom name for the classifier here
num_learners=10,
regularization=0.01,
optimizer_options={"simulator": True},
)

Validations

  • name:
    • ชื่อต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ไม่ควรมี classifier ที่มีชื่อเดียวกันอยู่แล้วใน shared data directory

3. Delete

action delete จะลบ classifier ออกจาก shared data directory

Inputs

NameTypeDescriptionRequired
actionstrชื่อของ action ต้องเป็น deleteYes
namestrชื่อของ classifier ที่จะลบYes

Usage

job = singularity.run(
action="delete",
name="classifier_name", # specify the name of the classifier to delete here
)

Validations

  • name:
    • ชื่อต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมี classifier ที่มีชื่อเดียวกันอยู่แล้วใน shared data directory

4. Fit

action fit ฝึก classifier โดยใช้ข้อมูลฝึกที่ให้มา

Inputs

NameTypeDescriptionRequired
actionstrชื่อของ action ต้องเป็น fitYes
namestrชื่อของ classifier ที่จะฝึกYes
Xarray or list or strข้อมูลฝึก อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
yarray or list or strค่า target สำหรับการฝึก อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
fit_paramsdictionaryพารามิเตอร์เพิ่มเติมที่จะส่งให้เมธอด fit ของ classifierNo
fit_params
NameTypeDescriptionRequiredDefault
validation_datatupleข้อมูล validation และ labelNoNone
pos_labelinteger or strlabel ของคลาสที่จะ map เป็น 1NoNone
optimization_datastrชุดข้อมูลที่ใช้ optimize ensemble สามารถเป็น: train, validation, bothNotrain

Usage

job = singularity.run(
action="fit",
name="classifier_name", # specify the name of the classifier to train here
X=X_train, # or "X_train.npy" if you uploaded it in the shared data directory
y=y_train, # or "y_train.npy" if you uploaded it in the shared data directory
fit_params={}, # define the fit parameters here
)

Validations

  • name:
    • ชื่อต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมี classifier ที่มีชื่อเดียวกันอยู่แล้วใน shared data directory

5. Predict

action predict ใช้เพื่อรับการทำนายแบบ hard และ soft (probabilities)

Inputs

NameTypeDescriptionRequired
actionstrชื่อของ action ต้องเป็น predictYes
namestrชื่อของ classifier ที่จะใช้Yes
Xarray or list or strข้อมูลทดสอบ อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
options["out"]strชื่อไฟล์ JSON สำหรับบันทึกการทำนายใน shared data directory ถ้าไม่ระบุ การทำนายจะถูกส่งคืนในผลลัพธ์ของ jobNo

Usage

job = singularity.run(
action="predict",
name="classifier_name", # specify the name of the classifier to use here
X=X_test, # or "X_test.npy" if you uploaded it to the shared data directory
options={
"out": "output.json",
},
)

Validations

  • name:
    • ชื่อต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมี classifier ที่มีชื่อเดียวกันอยู่แล้วใน shared data directory
  • options["out"]:
    • ชื่อไฟล์ต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมีนามสกุล .json

6. Fit-predict

action fit_predict ฝึก classifier โดยใช้ข้อมูลฝึก แล้วใช้มันเพื่อรับการทำนายแบบ hard และ soft (probabilities)

Inputs

NameTypeDescriptionRequired
actionstrชื่อของ action ต้องเป็น fit_predictYes
namestrชื่อของ classifier ที่จะใช้Yes
X_trainarray or list or strข้อมูลฝึก อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
y_trainarray or list or strค่า target สำหรับการฝึก อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
X_testarray or list or strข้อมูลทดสอบ อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
fit_paramsdictionaryพารามิเตอร์เพิ่มเติมที่จะส่งให้เมธอด fit ของ classifierNo
options["out"]strชื่อไฟล์ JSON สำหรับบันทึกการทำนายใน shared data directory ถ้าไม่ระบุ การทำนายจะถูกส่งคืนในผลลัพธ์ของ jobNo

Usage

job = singularity.run(
action="fit_predict",
name="classifier_name", # specify the name of the classifier to use here
X_train=X_train, # or "X_train.npy" if you uploaded it in the shared data directory
y_train=y_train, # or "y_train.npy" if you uploaded it in the shared data directory
X_test=X_test, # or "X_test.npy" if you uploaded it in the shared data directory
fit_params={}, # define the fit parameters here
options={
"out": "output.json",
},
)

Validations

  • name:

    • ชื่อต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมี classifier ที่มีชื่อเดียวกันอยู่แล้วใน shared data directory
  • options["out"]:

    • ชื่อไฟล์ต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมีนามสกุล .json

7. Create-fit-predict

action create_fit_predict จะสร้าง classifier ฝึกมันโดยใช้ข้อมูลฝึกที่ให้มา แล้วใช้มันเพื่อรับการทำนายแบบ hard และ soft (probabilities)

Inputs

NameTypeDescriptionRequired
actionstrชื่อของ action จาก create, list, fit, predict, fit_predict, create_fit_predict และ deleteYes
namestrชื่อของ classifier ที่จะใช้Yes
quantum_classifierstrประเภทของ classifier คือ QuantumEnhancedEnsembleClassifier ค่าเริ่มต้นคือ QuantumEnhancedEnsembleClassifierNo
X_trainarray or list or strข้อมูลฝึก อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
y_trainarray or list or strค่า target สำหรับการฝึก อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
X_testarray or list or strข้อมูลทดสอบ อาจเป็น NumPy array, list หรือ string ที่อ้างถึงชื่อไฟล์ใน shared data directoryYes
fit_paramsdictionaryพารามิเตอร์เพิ่มเติมที่จะส่งให้เมธอด fit ของ classifierNo
options["save"]booleanว่าจะบันทึก trained classifier ใน shared data directory หรือไม่ ค่าเริ่มต้นคือ TrueNo
options["out"]strชื่อไฟล์ JSON สำหรับบันทึกการทำนายใน shared data directory ถ้าไม่ระบุ การทำนายจะถูกส่งคืนในผลลัพธ์ของ jobNo

Usage

job = singularity.run(
action="create_fit_predict",
name="classifier_name", # specify your custom name for the classifier here
num_learners=10,
regularization=0.01,
optimizer_options={"simulator": True},
X_train=X_train, # or "X_train.npy" if you uploaded it in the shared data directory
y_train=y_train, # or "y_train.npy" if you uploaded it in the shared data directory
X_test=X_test, # or "X_test.npy" if you uploaded it in the shared data directory
fit_params={}, # define the fit parameters here
options={
"save": True,
"out": "output.json",
},
)

Validations

  • name:

    • ถ้า options["save"] ตั้งเป็น True:
      • ชื่อต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
      • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
      • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
      • ไม่ควรมี classifier ที่มีชื่อเดียวกันอยู่แล้วใน shared data directory
  • options["out"]:

    • ชื่อไฟล์ต้องไม่ซ้ำกัน เป็น string ที่มีความยาวไม่เกิน 64 ตัวอักษร
    • สามารถมีได้เฉพาะตัวอักษรและตัวเลข (alphanumeric) และขีดล่างเท่านั้น
    • ต้องเริ่มต้นด้วยตัวอักษรและห้ามลงท้ายด้วยขีดล่าง
    • ต้องมีนามสกุล .json

Get started

ยืนยันตัวตนด้วย IBM Quantum Platform API key แล้วเลือก Qiskit Function ดังนี้:

from qiskit_ibm_catalog import QiskitFunctionsCatalog

catalog = QiskitFunctionsCatalog(channel="ibm_quantum_platform")

# load function
singularity = catalog.load("multiverse/singularity")

Example

ในตัวอย่างนี้ เราจะใช้ฟังก์ชัน "Singularity Machine Learning - Classification" เพื่อจำแนกชุดข้อมูลที่ประกอบด้วยครึ่งวงกลมสองอันที่พันกันเป็นรูปพระจันทร์เสี้ยว ชุดข้อมูลนี้เป็นชุดสังเคราะห์ สองมิติ และมีป้ายกำกับแบบไบนารี ออกแบบมาให้ท้าทายกับอัลกอริทึม เช่น การจัดกลุ่มแบบ centroid-based และการจำแนกเชิงเส้น Moons dataset ในกระบวนการนี้ เราจะเรียนรู้วิธีสร้าง classifier, fit กับข้อมูล training, ใช้ทำนายข้อมูล test และลบ classifier เมื่อใช้งานเสร็จ ก่อนเริ่มต้น ต้องติดตั้ง scikit-learn ก่อน ติดตั้งด้วยคำสั่งต่อไปนี้:

python3 -m pip install scikit-learn

ทำตามขั้นตอนต่อไปนี้:

  1. สร้างชุดข้อมูลสังเคราะห์โดยใช้ฟังก์ชัน make_moons จาก scikit-learn
  2. อัปโหลดชุดข้อมูลสังเคราะห์ที่สร้างขึ้นไปยัง shared data directory
  3. สร้าง quantum-enhanced classifier โดยใช้ action create
  4. ดูรายการ classifier ของเราโดยใช้ action list
  5. เทรน classifier ด้วยข้อมูล train โดยใช้ action fit
  6. ใช้ classifier ที่เทรนแล้วเพื่อทำนายข้อมูล test โดยใช้ action predict
  7. ลบ classifier โดยใช้ action delete
  8. ล้างข้อมูลเมื่อทำเสร็จแล้ว

ขั้นตอนที่ 1. นำเข้าโมดูลที่จำเป็นและสร้างชุดข้อมูลสังเคราะห์ จากนั้นแบ่งเป็นชุดข้อมูล training และ test

# import the necessary modules for this example
import os
import tarfile
import numpy as np

# Import the make_moons and the train_test_split functions from scikit-learn
# to create a synthetic dataset and split it into training and test datasets
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split

# generate the synthetic dataset
X, y = make_moons(n_samples=10000)

# split the data into training and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# print the first 10 samples of the training dataset
print("Features:", X_train[:10, :])
print("Targets:", y_train[:10])
Features: [[-0.99958218  0.02890441]
[ 0.03285169 0.24578719]
[ 1.13127903 -0.49134546]
[ 1.86951286 0.00608971]
[ 0.20190413 0.97940529]
[ 0.8831311 0.46912627]
[-0.10819442 0.99412975]
[-0.20005727 0.97978421]
[-0.78775705 0.61598607]
[ 1.82453236 -0.0658148 ]]
Targets: [0 1 1 1 0 0 0 0 0 1]

ขั้นตอนที่ 2. บันทึกชุดข้อมูล training และ test ที่มีป้ายกำกับลงในดิสก์ในเครื่อง จากนั้นอัปโหลดไปยัง shared data directory

def make_tarfile(file_path, tar_file_name):
with tarfile.open(tar_file_name, "w") as tar:
tar.add(file_path, arcname=os.path.basename(file_path))

# save the training and test datasets on your local disk
np.save("X_train.npy", X_train)
np.save("y_train.npy", y_train)
np.save("X_test.npy", X_test)
np.save("y_test.npy", y_test)

# create tar files for the datasets
make_tarfile("X_train.npy", "X_train.npy.tar")
make_tarfile("y_train.npy", "y_train.npy.tar")
make_tarfile("X_test.npy", "X_test.npy.tar")
make_tarfile("y_test.npy", "y_test.npy.tar")

# upload the datasets to the shared data directory
catalog.file_upload("X_train.npy.tar", singularity)
catalog.file_upload("y_train.npy.tar", singularity)
catalog.file_upload("X_test.npy.tar", singularity)
catalog.file_upload("y_test.npy.tar", singularity)

# view/enlist the uploaded files in the shared data directory
print(catalog.files(singularity))
['X_test.npy.tar', 'X_train.npy.tar', 'y_test.npy.tar', 'y_train.npy.tar']

ขั้นตอนที่ 3. สร้าง quantum-enhanced classifier โดยใช้ action create

job = singularity.run(
action="create",
name="my_classifier",
num_learners=10,
learners_types=[
"DecisionTreeClassifier",
"KNeighborsClassifier",
],
learners_proportions=[0.5, 0.5],
learners_options=[{}, {}],
regularization=0.01,
weight_update_method="logarithmic",
sample_scaling=True,
optimizer_options={"simulator": True},
voting="soft",
prob_threshold=0.5,
)

print(job.result())
{'status': 'ok', 'message': 'Classifier created.', 'data': {}, 'metadata': {'resource_usage': {}}}
# list available classifiers using the list action
job = singularity.run(action="list")

print(job.result())

# you can also find your classifiers in the shared data directory with a *.pkl.tar extension
print(catalog.files(singularity))
{'status': 'ok', 'message': 'Classifiers listed.', 'data': {'classifiers': ['my_classifier']}, 'metadata': {'resource_usage': {}}}
['X_test.npy.tar', 'X_train.npy.tar', 'y_test.npy.tar', 'y_train.npy.tar', 'my_classifier.pkl.tar']

ขั้นตอนที่ 4. เทรน quantum-enhanced classifier โดยใช้ action fit

job = singularity.run(
action="fit",
name="my_classifier",
X="X_train.npy", # you do not need to specify the tar extension
y="y_train.npy", # you do not need to specify the tar extension
)

print(job.result())
{'status': 'ok', 'message': 'Classifier fitted.', 'data': {}, 'metadata': {'resource_usage': {'RUNNING: MAPPING': {'CPU_TIME': 8.45469617843628}, 'RUNNING: WAITING_QPU': {'CPU_TIME': 69.4949426651001}, 'RUNNING: POST_PROCESSING': {'CPU_TIME': 73.01881957054138}, 'RUNNING: EXECUTING_QPU': {'QPU_TIME': 75.4787163734436}}}}

ขั้นตอนที่ 5. รับผลการทำนายและความน่าจะเป็นจาก quantum-enhanced classifier โดยใช้ action predict

job = singularity.run(
action="predict",
name="my_classifier",
X="X_test.npy", # you do not need to specify the tar extension
)

result = job.result()

print("Action result status: ", result["status"])
print("Action result message: ", result["message"])
print("Predictions (first five results):", result["data"]["predictions"][:5])
print(
"Probabilities (first five results):", result["data"]["probabilities"][:5]
)
Action result status:  ok
Action result message: Classifier predicted.
Predictions (first five results): [0, 1, 0, 0, 1]
Probabilities (first five results): [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 1.0]]

ขั้นตอนที่ 6. ลบ quantum-enhanced classifier โดยใช้ action delete

job = singularity.run(
action="delete",
name="my_classifier",
)

# or you can delete from the shared data directory
# catalog.file_delete("my_classifier.pkl.tar", singularity)

print(job.result())
{'status': 'ok', 'message': 'Classifier deleted.', 'data': {}, 'metadata': {'resource_usage': {}}}

ขั้นตอนที่ 7. ล้างข้อมูลในไดเรกทอรีในเครื่องและไดเรกทอรีข้อมูลที่แชร์

# delete the numpy files from your local disk
os.remove("X_train.npy")
os.remove("y_train.npy")
os.remove("X_test.npy")
os.remove("y_test.npy")

# delete the tar files from your local disk
os.remove("X_train.npy.tar")
os.remove("y_train.npy.tar")
os.remove("X_test.npy.tar")
os.remove("y_test.npy.tar")

# delete the tar files from the shared data
catalog.file_delete("X_train.npy.tar", singularity)
catalog.file_delete("y_train.npy.tar", singularity)
catalog.file_delete("X_test.npy.tar", singularity)
catalog.file_delete("y_test.npy.tar", singularity)

Benchmarks

Benchmarks เหล่านี้แสดงให้เห็นว่า classifier สามารถบรรลุความแม่นยำสูงมากบนปัญหาที่ท้าทายได้ นอกจากนี้ยังแสดงให้เห็นว่าการเพิ่มจำนวน learner ใน ensemble (จำนวน Qubit) สามารถนำไปสู่ความแม่นยำที่เพิ่มขึ้นได้

"Classical accuracy" หมายถึงความแม่นยำที่ได้จากการใช้ state of the art แบบ classical ที่สอดคล้องกัน ซึ่งในกรณีนี้คือ AdaBoost classifier ที่อิงบน ensemble ขนาด 75 ส่วน "Quantum accuracy" หมายถึงความแม่นยำที่ได้จากการใช้ "Singularity Machine Learning - Classification"

ปัญหาขนาด Datasetขนาด Ensembleจำนวน Qubitความแม่นยำแบบ Classicalความแม่นยำแบบ Quantumการปรับปรุง
Grid stability5000 ตัวอย่าง, 12 features555576%91%15%
Grid stability5000 ตัวอย่าง, 12 features656576%92%16%
Grid stability5000 ตัวอย่าง, 12 features757576%94%18%
Grid stability5000 ตัวอย่าง, 12 features858576%94%18%
Grid stability5000 ตัวอย่าง, 12 features10010076%95%19%

เมื่อ hardware ของ quantum พัฒนาและขยายขนาดขึ้น ผลกระทบต่อ quantum classifier ของเราก็มีความสำคัญมากขึ้นเรื่อยๆ แม้ว่าจำนวน Qubit จะกำหนดข้อจำกัดในขนาดของ ensemble ที่สามารถใช้ได้ แต่ก็ไม่ได้จำกัดปริมาณข้อมูลที่สามารถประมวลผลได้ ความสามารถอันทรงพลังนี้ทำให้ classifier สามารถจัดการ dataset ที่มีข้อมูลหลายล้านจุดและ features หลายพันรายการได้อย่างมีประสิทธิภาพ สิ่งสำคัญคือข้อจำกัดที่เกี่ยวข้องกับขนาด ensemble สามารถแก้ไขได้ผ่านการนำ classifier เวอร์ชันขนาดใหญ่มาใช้ การใช้ประโยชน์จากแนวทาง iterative outer-loop ทำให้ ensemble สามารถขยายขนาดได้แบบ dynamic เพิ่มความยืดหยุ่นและประสิทธิภาพโดยรวม อย่างไรก็ตาม ควรทราบว่าฟีเจอร์นี้ยังไม่ได้ถูกนำมาใช้ในเวอร์ชันปัจจุบันของ classifier

Changelog

4 มิถุนายน 2025

  • อัปเกรด QuantumEnhancedEnsembleClassifier ด้วยการอัปเดตดังต่อไปนี้:
    • เพิ่ม onsite/alpha regularization สามารถกำหนด regularization_type เป็น onsite หรือ alpha ได้
    • เพิ่ม auto-regularization สามารถตั้ง regularization เป็น auto เพื่อใช้ auto-regularization
    • เพิ่มพารามิเตอร์ optimization_data ใน method fit เพื่อเลือกข้อมูลสำหรับ quantum optimization สามารถใช้หนึ่งในตัวเลือกเหล่านี้: train, validation, หรือ both
    • ปรับปรุงประสิทธิภาพโดยรวม
  • เพิ่มการติดตามสถานะอย่างละเอียดสำหรับ job ที่กำลังทำงาน

20 พฤษภาคม 2025

  • ปรับมาตรฐานการจัดการข้อผิดพลาด

18 มีนาคม 2025

  • อัปเกรด qiskit-serverless เป็น 0.20.0 และ base image เป็น 0.20.1

14 กุมภาพันธ์ 2025

  • อัปเกรด base image เป็น 0.19.1

6 กุมภาพันธ์ 2025

  • อัปเกรด qiskit-serverless เป็น 0.19.0 และ base image เป็น 0.19.0

13 พฤศจิกายน 2024

  • เปิดตัว Singularity Machine Learning - Classification

Get support

สำหรับคำถามใดๆ ติดต่อ Multiverse Computing

อย่าลืมใส่ข้อมูลต่อไปนี้:

  • Qiskit Function Job ID (job.job_id)
  • คำอธิบายปัญหาอย่างละเอียด
  • ข้อความหรือรหัสข้อผิดพลาดที่เกี่ยวข้อง
  • ขั้นตอนในการทำซ้ำปัญหา

Next steps

Source: IBM Quantum docs — updated 5 พ.ค. 2569
English version on doQumentation — updated 7 พ.ค. 2569
This translation based on the English version of 11 มี.ค. 2569