Skip to content

Commit 551bdd4

Browse files
committed
Update Hands-On-Quantum-Machine-Learning-With-Python-Vol-1
1 parent aa00aef commit 551bdd4

File tree

3 files changed

+931
-0
lines changed

3 files changed

+931
-0
lines changed

chapter_03.ipynb

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,261 @@
606606
"import random\n",
607607
"random.seed(a=None, version=2)"
608608
]
609+
},
610+
{
611+
"source": [
612+
"## Section 3.6 Variational Hybrid Quantum-Classical Algorithm "
613+
],
614+
"cell_type": "markdown",
615+
"metadata": {}
616+
},
617+
{
618+
"cell_type": "code",
619+
"execution_count": 24,
620+
"metadata": {},
621+
"outputs": [],
622+
"source": [
623+
"# Listing 3.19: Return statement of pqc-classify\n",
624+
"def pqc_classify(backend, passenger_state):\n",
625+
" # ...\n",
626+
" \n",
627+
" # get the bit 0 or 1\n",
628+
" return int(list(map(lambda item: item[0], counts.items()))[0])"
629+
]
630+
},
631+
{
632+
"cell_type": "code",
633+
"execution_count": 25,
634+
"metadata": {},
635+
"outputs": [],
636+
"source": [
637+
"# Listing 3.20: Pre‐processing template\n",
638+
"def pre_process(passenger):\n",
639+
" \"\"\"\n",
640+
" passenger -- the normalized (array of numeric data) passenger data\n",
641+
" returns a valid quantum state\n",
642+
" \"\"\"\n",
643+
" quantum_state = [1/sqrt(2), 1/sqrt(2)] \n",
644+
" return quantum_state"
645+
]
646+
},
647+
{
648+
"cell_type": "code",
649+
"execution_count": 26,
650+
"metadata": {},
651+
"outputs": [],
652+
"source": [
653+
"# Listing 3.21: The parameterized quantum circuit\n",
654+
"def pqc(backend, quantum_state):\n",
655+
" \"\"\"\n",
656+
" backend -- a qiskit backend to run the quantum circuit at\n",
657+
" quantum_state -- a valid quantum state vector \n",
658+
" returns the counts of the measurement\n",
659+
" \"\"\"\n",
660+
"\n",
661+
" # Create a quantum circuit with one qubit\n",
662+
" qc = QuantumCircuit(1) \n",
663+
"\n",
664+
" # Define state |Psi> and initialize the circuit\n",
665+
" qc.initialize(quantum_state, 0)\n",
666+
" \n",
667+
" # Measure the qubit\n",
668+
" qc.measure_all()\n",
669+
"\n",
670+
" # run the quantum circuit\n",
671+
" result=execute(qc,backend).result()\n",
672+
"\n",
673+
" # get the counts, these are either {'0': 1} or {'1': 1}\n",
674+
" counts=result.get_counts(qc)\n",
675+
"\n",
676+
" return counts"
677+
]
678+
},
679+
{
680+
"cell_type": "code",
681+
"execution_count": 27,
682+
"metadata": {},
683+
"outputs": [],
684+
"source": [
685+
"# Listing 3.22: Post‐processing\n",
686+
"def post_process(counts):\n",
687+
" \"\"\"\n",
688+
" counts -- the result of the quantum circuit execution\n",
689+
" returns the prediction\n",
690+
" \"\"\"\n",
691+
" return int(list(map(lambda item: item[0], counts.items()))[0])"
692+
]
693+
},
694+
{
695+
"cell_type": "code",
696+
"execution_count": 29,
697+
"metadata": {},
698+
"outputs": [
699+
{
700+
"output_type": "stream",
701+
"name": "stdout",
702+
"text": [
703+
"The precision score of the Variational classifier is 0.40\nThe recall score of the Variational classifier is 0.52\nThe specificity score of the Variational classifier is 0.53\nThe npv score of the Variational classifier is 0.65\nThe information level is: 0.53\n"
704+
]
705+
}
706+
],
707+
"source": [
708+
"# Listing 3.23: The scores of the random quantum classifier\n",
709+
"# Tell Qiskit how to simulate our circuit\n",
710+
"backend = Aer.get_backend('statevector_simulator') \n",
711+
"\n",
712+
"classifier_report(\n",
713+
" \"Variational\",\n",
714+
" run,\n",
715+
" lambda passenger: post_process(pqc(backend, pre_process(passenger))),\n",
716+
" train_input,\n",
717+
" train_labels)"
718+
]
719+
},
720+
{
721+
"cell_type": "code",
722+
"execution_count": 30,
723+
"metadata": {},
724+
"outputs": [],
725+
"source": [
726+
"# Listing 3.24: weigh a passenger's feature\n",
727+
"def weigh_feature(feature, weight):\n",
728+
" \"\"\"\n",
729+
" feature -- the single value of a passenger's feature\n",
730+
" weight -- the overall weight of this feature\n",
731+
" returns the weighted feature \n",
732+
" \"\"\"\n",
733+
" return feature*weight"
734+
]
735+
},
736+
{
737+
"cell_type": "code",
738+
"execution_count": 31,
739+
"metadata": {},
740+
"outputs": [],
741+
"source": [
742+
"# Listing 3.25: Calculate the overall probability\n",
743+
"from functools import reduce\n",
744+
"\n",
745+
"def get_overall_probability(features, weights):\n",
746+
" \"\"\"\n",
747+
" features -- list of the features of a passenger\n",
748+
" weights -- list of all features' weights\n",
749+
" \"\"\"\n",
750+
" return reduce(\n",
751+
" lambda result, data: result + weigh_feature(*data),\n",
752+
" zip(features, weights),\n",
753+
" 0\n",
754+
" )"
755+
]
756+
},
757+
{
758+
"cell_type": "code",
759+
"execution_count": 32,
760+
"metadata": {},
761+
"outputs": [
762+
{
763+
"output_type": "execute_result",
764+
"data": {
765+
"text/plain": [
766+
"[-0.3199865971328056,\n",
767+
" -0.509502431337675,\n",
768+
" -0.03330984969276768,\n",
769+
" 0.08830880238697662,\n",
770+
" 0.13682508666206933,\n",
771+
" 0.3023305198766798,\n",
772+
" -0.13624682982687314]"
773+
]
774+
},
775+
"metadata": {},
776+
"execution_count": 32
777+
}
778+
],
779+
"source": [
780+
"# Listing 3.26: Calculate the correlation coefficients\n",
781+
"from scipy.stats import spearmanr\n",
782+
"\n",
783+
"# separate the training data into a list of the columns\n",
784+
"columns = [list(map(lambda passenger: passenger[i], train_input)) for i in range(0,7)]\n",
785+
"\n",
786+
"# calculate the correlation coefficient for each column\n",
787+
"correlations = list(map(lambda col: spearmanr(col, train_labels)[0], columns))\n",
788+
"correlations"
789+
]
790+
},
791+
{
792+
"cell_type": "code",
793+
"execution_count": 33,
794+
"metadata": {},
795+
"outputs": [],
796+
"source": [
797+
"# Listing 3.27: The weighting pre‐processing\n",
798+
"from math import pi, sin, cos\n",
799+
"\n",
800+
"def get_state (theta):\n",
801+
" \"\"\"returns a valid state vector from angle theta\"\"\"\n",
802+
" return [cos(theta/2), sin(theta/2)]\n",
803+
"\n",
804+
"def pre_process_weighted(passenger):\n",
805+
" \"\"\"\n",
806+
" passenger -- the normalized (array of numeric data) passenger data\n",
807+
" returns a valid quantum state\n",
808+
" \"\"\"\n",
809+
"\n",
810+
" # caluclate the overall probability\n",
811+
" mu = get_overall_probability(passenger, correlations)\n",
812+
" \n",
813+
" # theta between 0 (|0>) and pi (|1>)\n",
814+
" quantum_state = get_state((1-mu)*pi)\n",
815+
"\n",
816+
" return quantum_state"
817+
]
818+
},
819+
{
820+
"cell_type": "code",
821+
"execution_count": 36,
822+
"metadata": {},
823+
"outputs": [
824+
{
825+
"output_type": "stream",
826+
"name": "stdout",
827+
"text": [
828+
"The precision score of the Variational classifier is 0.69\nThe recall score of the Variational classifier is 0.62\nThe specificity score of the Variational classifier is 0.83\nThe npv score of the Variational classifier is 0.78\nThe information level is: 0.73\n"
829+
]
830+
}
831+
],
832+
"source": [
833+
"# Listing 3.28: Run the PQC with the weighted pre‐processing\n",
834+
"backend = Aer.get_backend('statevector_simulator') \n",
835+
"\n",
836+
"classifier_report(\"Variational\", \n",
837+
" run,\n",
838+
" lambda passenger: post_process(pqc(backend, pre_process_weighted(passenger))),\n",
839+
" train_input,\n",
840+
" train_labels)"
841+
]
842+
},
843+
{
844+
"cell_type": "code",
845+
"execution_count": 37,
846+
"metadata": {},
847+
"outputs": [
848+
{
849+
"output_type": "stream",
850+
"name": "stdout",
851+
"text": [
852+
"The precision score of the Variational-Test classifier is 0.67\nThe recall score of the Variational-Test classifier is 0.73\nThe specificity score of the Variational-Test classifier is 0.75\nThe npv score of the Variational-Test classifier is 0.80\nThe information level is: 0.74\n"
853+
]
854+
}
855+
],
856+
"source": [
857+
"# Listing 3.29: Test the PQC‐based classifier on data it has not seen before\n",
858+
"classifier_report(\"Variational-Test\", \n",
859+
" run,\n",
860+
" lambda passenger: post_process(pqc(backend, pre_process_weighted(passenger))),\n",
861+
" test_input,\n",
862+
" test_labels)"
863+
]
609864
}
610865
]
611866
}

0 commit comments

Comments
 (0)