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 a notebook for Quantum Neuron #242

Closed
wants to merge 3 commits into from
Closed
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
740 changes: 740 additions & 0 deletions examples/pennylane/7_Quantum_neuron/7_Quantum_neuron.ipynb

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions examples/pennylane/7_Quantum_neuron/inputs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
120 changes: 120 additions & 0 deletions examples/pennylane/7_Quantum_neuron/qn/quantum_neuron_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import json
import os
import time
import sys
import ast

import numpy as np
import matplotlib.pyplot as plt
from typing import List
from braket.aws import AwsQuantumJob, AwsSession
from braket.jobs.image_uris import Framework, retrieve_image
from braket.jobs import load_job_checkpoint, save_job_checkpoint, save_job_result
from braket.tracking import Tracker

import pennylane as qml

def init_pl_device(device_arn, n_nodes, shots, max_parallel):
return qml.device(
"braket.aws.qubit",
device_arn=device_arn,
wires=n_nodes,
shots=shots,
# Set s3_destination_folder=None to output task results to a default folder
s3_destination_folder=None,
parallel=True,
max_parallel=max_parallel,
# poll_timeout_seconds=30,
)

def af(inputs:str, weights, bias, ancilla, output, n_nodes): # af: activation function
for qubit in range(len(inputs)):
if(inputs[qubit]=='1'):
qml.PauliX(qubit)

for qubit in range(len(inputs)):
qml.CRY(phi=2*weights[qubit], wires=[qubit, ancilla])

qml.RY(2*bias, wires=ancilla)

qml.CY(wires=[ancilla, output])
qml.RZ(phi=-np.pi/2, wires=ancilla)

for qubit in range(len(inputs)):
qml.CRY(phi=-2*weights[qubit], wires=[qubit, ancilla]) # note '-(minus)'

qml.RY(-2*bias, wires=ancilla) # note '-(minus)'

return [qml.sample(qml.PauliZ(i)) for i in range(n_nodes)]

def quantum_neuron(inputs:str, weights, bias, n_nodes, dev):
ancilla = len(weights) # ID of an ancilla qubit
output = len(weights) + 1 # ID of an output qubit

theta = np.inner(np.array(list(inputs), dtype=int), np.array(weights)) + bias # linear comination with numpy
theta = theta.item() # Convert numpy array to native python float-type

af_circuit = qml.QNode(af, dev)
sample = af_circuit(inputs, weights, bias, ancilla, output, n_nodes)
sample = sample.T
sample = (1 - sample.numpy()) / 2

adopted_sample = sample[sample[:,ancilla] == 0]

count_0 = len(adopted_sample[adopted_sample[:,output] == 0])
count_1 = len(adopted_sample[adopted_sample[:,output] == 1])

p_0 = count_0 / (count_0 + count_1)

q_theta = np.arccos(np.sqrt(p_0))

return theta, q_theta

def main():
t = Tracker().start()

input_dir = os.environ["AMZN_BRAKET_INPUT_DIR"]
output_dir = os.environ["AMZN_BRAKET_JOB_RESULTS_DIR"]
job_name = os.environ["AMZN_BRAKET_JOB_NAME"] # noqa
checkpoint_dir = os.environ["AMZN_BRAKET_CHECKPOINT_DIR"] # noqa
hp_file = os.environ["AMZN_BRAKET_HP_FILE"]
device_arn = os.environ["AMZN_BRAKET_DEVICE_ARN"]

# Read the hyperparameters
with open(hp_file, "r") as f:
hyperparams = json.load(f)

n_inputs = int(hyperparams["n_inputs"])
weights = ast.literal_eval(hyperparams["weights"])
bias = float(hyperparams["bias"])
shots = int(hyperparams["shots"])
interface = hyperparams["interface"]
max_parallel = int(hyperparams["max_parallel"])

if "copy_checkpoints_from_job" in hyperparams:
copy_checkpoints_from_job = hyperparams["copy_checkpoints_from_job"].split("/", 2)[-1]
else:
copy_checkpoints_from_job = None

# Read input strings from input file
with open(f"{input_dir}/input/inputs.txt") as f:
inputs = [s.strip() for s in f.readlines()]

# Prepare quantum neuron circuit
n_nodes = n_inputs+2 # +2: ancilla and output qubit

# Run quantum neuron circuit
dev = init_pl_device(device_arn, n_nodes, shots, max_parallel)
theta_list = []
q_theta_list = []

for i in range(2**n_inputs):
theta, q_theta = quantum_neuron(inputs[i], weights, bias, n_nodes, dev)

theta_list.append(theta)
q_theta_list.append(q_theta)

save_job_result({"theta_list": theta_list, "q_theta_list": q_theta_list, "task summary": t.quantum_tasks_statistics(), "estimated cost": t.qpu_tasks_cost() + t.simulator_tasks_cost()})

if __name__ == "__main__":
main()