<jupyter_start><jupyter_text><jupyter_code>pip install pennylane
pip install torch
pip install tensorflow
pip install sklearn
pip install pennylane-qiskit
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=2)
@qml.qnode(device=dev)
def cos_func(x, w):
  qml.RX(x, wires=0)
  qml.templates.BasicEntanglerLayers(w, wires=range(2))
  return qml.expval(qml.PauliZ(0))

layer = 4
weights = qml.init.basic_entangler_layers_uniform(layer, 2)

xs = np.linspace(-np.pi, 4*np.pi, requires_grad=False)
ys = np.cos(xs)
opt = qml.AdamOptimizer()
epochs = 10

for epoch in range(epochs):
  for x, y in zip(xs, ys):
    cost = lambda weights:(cos_func(x, weights) - y) ** 2
    weights = opt.step(cost, weights)

ys_trained = [cos_func(x, weights) for x in xs]

import matplotlib.pyplot as plt

plt.figure()
plt.plot(xs, ys_trained, marker="o", label="Cos(x")
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Preparing GHZ stateUsing the Autograd interface, train a circuit to prepare the 3-qubit W state:$|W> = {1/sqrt(3)}(001|> + |010> + |100>)<jupyter_code>qubits = 3

w = np.array([0, 1, 1, 0, 1, 0, 0, 0]) / np.sqrt(3)
w_projector = w[:, np.newaxis] * w
w_decomp = qml.utils.decompose_hamiltonian(w_projector)
H = qml.Hamiltonian(*w_decomp)

def prepare_w(weights, wires):
    qml.templates.StronglyEntanglingLayers(weights, wires=wires)

dev = qml.device("default.qubit", wires=qubits)
qnodes = qml.map(prepare_w, H.ops, dev)
w_overlap = qml.dot(H.coeffs, qnodes)

layers = 4
weights = qml.init.strong_ent_layers_uniform(layers, qubits)

opt = qml.RMSPropOptimizer()

epochs = 50

for i in range(epochs):
    weights = opt.step(lambda weights: -w_overlap(weights), weights)
    if i % 5 == 0:
        print(i, w_overlap(weights))

output_overlap = w_overlap(weights)
output_state = np.round(dev.state, 3)<jupyter_output><empty_output><jupyter_text>Quantum-based Optimization<jupyter_code>dev = qml.device('default.qubit', wires=1)

@qml.qnode(dev)
def rotation(thetas):
    qml.RX(1, wires=0)
    qml.RZ(1, wires=0)
    
    qml.RX(thetas[0], wires=0)
    qml.RY(thetas[1], wires=0)

    return qml.expval(qml.PauliZ(0))

opt = qml.RotoselectOptimizer()


import sklearn.datasets

data = sklearn.datasets.load_iris()
x = data["data"]
y = data["target"]

np.random.seed(1967)
x, y = zip(*np.random.permutation(list(zip(x, y))))

split = 125

x_train = x[:split]
x_test = x[split:]
y_train = y[:split]
y_test = y[split:]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1501597444089457, 'fraction_numerical': 0.02196485623003195, 'mean_word_length': 5.169950738916256, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '535', 'license': 'MIT', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1032, 'n_words': 211, 'path': 'Xanadu3.ipynb', 'repo_name': 'olgOk/XanaduTraining', 'size': '2504'}