Add `qml.FlipSign` subroutine template
See original GitHub issueFeature details
It would be great to have a FlipSign operator that supports an integer n or its binary representation, such that:
FlipSign(n)|m> = -|m> if m = n FlipSign(n)|m> = |m> if m != n
That is, to change the sign of a specified state. This would facilitate the process of oracle creation
Implementation
Let guess, we have the binary representation bin_n = [1,0,0,0] and wires = range(4)
The template could have the following form:
if bin_n[-1] == 0:
qml.PauliX(wires = wires[-1])
qml.ctrl(qml.PauliZ, control = wires[:-1], control_values = n[:-1])(wires = wires[-1])
if bin_n[-1] == 0:
qml.PauliX(wires = wires[-1])
We should check that the dimensions of bin_n and wires are the same.
Here I leave a subroutine to pass n to list that can be useful 😃
n = 21
wires = range(5)
bin_string = f"{n:b}".zfill(len(wires))
bin_n = [int(i) for i in bin_string]
print(bin_n)
[1,0,1,0,1]
Steps to take
Implementation
- Create a new subroutine template in the subroutines folder named
FlipSignand make it available viaqml.FlipSign; - Allow
qml.FlipSignto appear in the relevant parts of the documentation.
See other subroutine templates from the above-linked folder for the details of the implementation and documentation
Testing
Add tests to a new test file to the templates/test_subroutines/ folder. The testing should follow similar to the testing of other subroutines.
How important would you say this feature is?
1: Not important. Would be nice to have.
Additional information
Final example
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires = 0)
qml.FlipSign(1, wires = 0)
qml.Hadamard(wires = 0)
return qml.sample()
circuit()
[1]
|0> --> |0> + |1> --> |0> - |1> --> |1>
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:10 (5 by maintainers)

Top Related StackOverflow Question
Yes! Closed 🚀
Hi @KetpuntoG ,
No problem at all!!! I keep working on it.💪
Thanks for the pinpoint path!
[EDIT] Finally here is my file but I think that I have do not understand properly your indications, I think I misunderstood something or I missed something though🤦
Besides this, I still do not understand the test class, can I have some hint about what to do there? Anyway, I am still digging into the code…
Thanks