H.6.3 (b) Controlled Subcircuit

Hello. I cannot find the error in my implementation of the controlled sub-circuit in H6? Any help would be appreciated.

Hi @qreg, your solution is very close :slight_smile:

qml.ctrl is a transform that takes an operation or quantum function as input, and returns a function as output in which every gate is controlled by the specified wire:

controlled_function = qml.ctrl(original_function, control=wire)

But this is just a function, it must then be called in order to actually implement the gates.

Please let us know if there’s anything else we can clarify!

1 Like

Thank you. I have modified the code but still cannot make it work. The attached code results in "Incorrect: your circuit doesn’t give the right amplitudes.


Hi @qreg, I think that might be from the second line in subcircuit. If you have a numpy array x and take x ** 2, it will compute the element-wise square. For example,

>>> a = np.array([[1, 2], [3, 4]])
>>> a ** 2
array([[1, 4], [9, 16]])

So you’ll need to make some adjustments in order to get the square of the matrix itself.

(BTW you might also have to remove the pass statement)

1 Like

Yes, thank you so much, That did it.