Hi, can anybody tell me how to build a classifier model to learn AND gate? on PennyLane documentation, there is an example on rotation(how to train a quantum ciruit to act as NOT gate). but my question is about how to train a model to get AND gate in PennyLane?

Hi EMY91,

Just to clarify, you want to train a quantum circuit which does the following (e.g., using the computational basis)?:

00->0

01->0

10->0

11->1

If so, what you can do is prepare a â€śdatasetâ€ť of input/output relations corresponding to this truth table. Youâ€™ll need a two-wire circuit (since AND requires two inputs).

You can use a simple embedding rule: start with all wires in the 0 state. If an input has a 1, flip that qubit.

Then choose a circuit which you want to use as a model (it might include single qubit rotations and entangling gates, e.g., CNOTs â€“ this is up to you).

Then measure `qml.expval.PauliZ`

at the output of one of the wires (doesnâ€™t matter which one).

After the quantum circuit, you might want to convert the measurement result (between -1 and 1) to the range between 0 and 1. Then you can use a simple mean squared cost function to match the observed outputs with the target outputs.

Finally, you sum over all â€śdatapointsâ€ť i.e., the 4 known values of your truth table. By minimizing this final cost function, you should (hopefully â€“ depends on how you choose your circuit) be able to fit an AND gate in the computational basis.