Hi @cuhrazatee,

Perhaps you can try using qml.probs. This is autodifferentiable and can be used to return the probabilities of each bitstring output of a quantum circuit. This is a quick example of how to implement the workflow above with simple functions. This essentially maximizes the probability of sampling 1 from the quantum circuit.

```
import pennylane as qml
dev = qml.device('default.qubit',wires=1)
@qml.qnode(dev)
def quantum_circuit(params):
qml.RX(params,wires=0)
return qml.probs(wires=0)
def neural_network(params):
#Use a neural network to process the input
#Here we just multiply by 2 for simplicity
result = params*2
return result
def cost_fn(params):
result_circuit = quantum_circuit(params)[0]
#index 0 contains the probability of sampling |0>
result_neural_network = neural_network(result_circuit)
#Perform desired cost evaluation
#Here we just multiply by 2 for simplicity
cost = result_neural_network*2
return cost
#optimizer
opt = qml.GradientDescentOptimizer(stepsize=0.4)
#initial guess for theta
theta = 0.1
for i in range (100):
theta, prev_cost = opt.step_and_cost(cost_fn,theta)
if i%10 == 0:
print(f'Step = {i}, Theta = {theta:.2f}, Cost = {cost_fn(theta)}')
print(f'Probability of sampling 0: {quantum_circuit(theta)[0]:.2f} \nProbability of sampling 1: {quantum_circuit(theta)[1]:.2f}')
```

Results:

```
Step = 0, Theta = 0.02, Cost = 4.0
Step = 10, Theta = 2.04, Cost = 1.128
Step = 20, Theta = 3.15, Cost = 0.0
Step = 30, Theta = 3.15, Cost = 0.0
Step = 40, Theta = 3.15, Cost = 0.0
Step = 50, Theta = 3.15, Cost = 0.0
Step = 60, Theta = 3.16, Cost = 0.0
Step = 70, Theta = 3.18, Cost = 0.0
Step = 80, Theta = 3.13, Cost = 0.0
Step = 90, Theta = 3.15, Cost = 0.0
Probability of sampling 0: 0.00
Probability of sampling 1: 1.00
```