-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copied qml_200_WhoLikesTheBeatles problem templates
- Loading branch information
xanadu-qubot
committed
Feb 14, 2022
1 parent
8381728
commit 0323f90
Showing
6 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0,0.4210528986556 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
23,150,1,13,20,YES,24,200,YES,14,130,NO,50,20,NO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0,0.9264606253589677 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
23,10,1,20,100,YES,25,20,YES,14,130,NO,50,20,YES,60,300,YES,45,30,NO,33,20,NO |
Binary file not shown.
76 changes: 76 additions & 0 deletions
76
Coding_Challenges/qml_200_WhoLikesTheBeatles_template/who_likes_the_beatles_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#! /usr/bin/python3 | ||
|
||
import sys | ||
from pennylane import numpy as np | ||
import pennylane as qml | ||
|
||
|
||
def distance(A, B): | ||
"""Function that returns the distance between two vectors. | ||
Args: | ||
- A (list[int]): person's information: [age, minutes spent watching TV]. | ||
- B (list[int]): person's information: [age, minutes spent watching TV]. | ||
Returns: | ||
- (float): distance between the two feature vectors. | ||
""" | ||
|
||
# QHACK # | ||
|
||
# The Swap test is a method that allows you to calculate |<A|B>|^2 , you could use it to help you. | ||
# The qml.AmplitudeEmbedding operator could help you too. | ||
|
||
# dev = qml.device("default.qubit", ... | ||
# @qml.qnode(dev) | ||
|
||
# QHACK # | ||
|
||
|
||
def predict(dataset, new, k): | ||
"""Function that given a dataset, determines if a new person do like Beatles or not. | ||
Args: | ||
- dataset (list): List with the age, minutes that different people watch TV, and if they like Beatles. | ||
- new (list(int)): Age and TV minutes of the person we want to classify. | ||
- k (int): number of nearby neighbors to be taken into account. | ||
Returns: | ||
- (str): "YES" if they like Beatles, "NO" otherwise. | ||
""" | ||
|
||
# DO NOT MODIFY anything in this code block | ||
|
||
def k_nearest_classes(): | ||
"""Function that returns a list of k near neighbors.""" | ||
distances = [] | ||
for data in dataset: | ||
distances.append(distance(data[0], new)) | ||
nearest = [] | ||
for _ in range(k): | ||
indx = np.argmin(distances) | ||
nearest.append(indx) | ||
distances[indx] += 2 | ||
|
||
return [dataset[i][1] for i in nearest] | ||
|
||
output = k_nearest_classes() | ||
|
||
return ( | ||
"YES" if len([i for i in output if i == "YES"]) > len(output) / 2 else "NO", | ||
float(distance(dataset[0][0], new)), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
# DO NOT MODIFY anything in this code block | ||
inputs = sys.stdin.read().split(",") | ||
dataset = [] | ||
new = [int(inputs[0]), int(inputs[1])] | ||
k = int(inputs[2]) | ||
for i in range(3, len(inputs), 3): | ||
dataset.append([[int(inputs[i + 0]), int(inputs[i + 1])], str(inputs[i + 2])]) | ||
|
||
output = predict(dataset, new, k) | ||
sol = 0 if output[0] == "YES" else 1 | ||
print(f"{sol},{output[1]}") |