Skip to content

Commit

Permalink
Copied qml_200_WhoLikesTheBeatles problem templates
Browse files Browse the repository at this point in the history
  • Loading branch information
xanadu-qubot committed Feb 14, 2022
1 parent 8381728 commit 0323f90
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0,0.4210528986556
1 change: 1 addition & 0 deletions Coding_Challenges/qml_200_WhoLikesTheBeatles_template/1.in
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0,0.9264606253589677
1 change: 1 addition & 0 deletions Coding_Challenges/qml_200_WhoLikesTheBeatles_template/2.in
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.
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]}")

0 comments on commit 0323f90

Please sign in to comment.