qiskit-community / qiskit-machine-learning

Quantum Machine Learning
https://qiskit-community.github.io/qiskit-machine-learning/
Apache License 2.0
677 stars 326 forks source link

Cannot apply instruction with classical bits #576

Closed plutoniumm closed 1 year ago

plutoniumm commented 1 year ago

Environment

What is happening?

Circuit is not returning the correct sampled value for EstimatorQNN with SparsePauliOp observable with 12 qubits. However I ran This Kaggle Example and it ran perfectly

How can we reproduce the issue?

# CIRCUIT
qubits = 12 
circuit = qiskit.QuantumCircuit(qubits);
all_qubits = [i for i in range(qubits)]
circuit.h(all_qubits)
# Parametrisation
inputs = [qiskit.circuit.Parameter(f'x_{i}') for i in all_qubits]
weights = [qiskit.circuit.Parameter(f'w_{i}') for i in all_qubits]

for i in all_qubits:
    circuit.rx(inputs[i], i)
for i in all_qubits:
    circuit.cx(i, ((i+1)%12))
circuit.barrier()
circuit.h(all_qubits)
for i in all_qubits:
    circuit.rx(weights[i], i)
circuit.h(all_qubits)
circuit.measure_all()
circuit.draw('mpl')

Basically it did H → Rx → Entable → H → Rx → H on all qubits

With SparsePauliOp then observables = [SparsePauliOp.from_list([("Z" * qubits, 1+0j)])];

# ESTIMATOR
estimator_qnn = EstimatorQNN(
    circuit=circuit, observables=observables, input_params=inputs, weight_params=weights
)
init_inputs = algorithm_globals.random.random(estimator_qnn.num_inputs)
init_weights = algorithm_globals.random.random(estimator_qnn.num_weights)

# print(f"Number of input features for EstimatorQNN: {estimator_qnn.num_inputs} \nInput: {init_inputs}")
# print(f"Number of trainable weights for EstimatorQNN: {estimator_qnn.num_weights} \nWeights: {init_weights}")

estimator_forward = estimator_qnn.forward(init_inputs, init_weights)

print(f"Forward pass result for EstimatorQNN: {estimator_forward}. \nShape: {estimator_forward.shape}")

This returns a QiskitError: 'Cannot apply instruction with classical bits: measure' with the default Statevector sim method

What should happen?

An estimate value should come out as a list

Any suggestions?

I'm not sure where the error it it may just be me doing some very stupid mistake since the Kaggle ex ran and me changing the circuit didn't, so I apologise for that case in advance. Thanks for the library I'm really enjoying exploring it :)

AbdullahKazi500 commented 1 year ago

The error message you're seeing (QiskitError: 'Cannot apply instruction with classical bits: measure') is because you are trying to simulate the circuit using a StatevectorSimulator, which does not support measurement. Since you are using EstimatorQNN, which is a parameterized quantum circuit for quantum machine learning, you need to use a quantum simulator that supports measurement. the output of the EstimatorQNN is not a single value, but rather a vector of values, one for each observable. In this case, you are using a single observable (SparsePauliOp.from_list([("Z" * qubits, 1+0j)])), so the output should be a single value. although I am not sure on this @plutoniumm

plutoniumm commented 1 year ago

So i might really be using this wrong, can you (/Anyone else) help me out with some Papers or applications of this? I can't seem to find much in the docs

plutoniumm commented 1 year ago

Hey sorry i figured out how to use this it seems i was using it in the wrong context alltogether it's not meant to be a layer in an NN