AND Circuit Learning


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)?:

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.