Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 5486ea2

Browse files
lmrazoreatcNickolas
authored andcommitted
Refactor duplicate code in Grover's algorithm kata. (#39)
1 parent 13705d3 commit 5486ea2

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

GroversAlgorithm/ReferenceImplementation.qs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,22 @@ namespace Quantum.Kata.GroversAlgorithm
3636
{
3737
// flip the bits in odd (0-based positions),
3838
// so that the condition for flipping the state of the target qubit is "query register is in 1...1 state"
39-
for (n in 0..Length(queryRegister)-1)
40-
{
41-
if (n % 2 == 1)
42-
{
43-
X(queryRegister[n]);
44-
}
45-
}
39+
FlipOddPositionBits_Reference(queryRegister);
4640
(Controlled X)(queryRegister, target);
47-
for (n in 0..Length(queryRegister)-1)
41+
(Adjoint FlipOddPositionBits_Reference)(queryRegister);
42+
}
43+
adjoint auto;
44+
}
45+
46+
operation FlipOddPositionBits_Reference (register : Qubit[]) : ()
47+
{
48+
body
49+
{
50+
for (i in 0..Length(register) - 1)
4851
{
49-
if (n % 2 == 1)
52+
if (i % 2 == 1)
5053
{
51-
X(queryRegister[n]);
54+
X(register[i]);
5255
}
5356
}
5457
}

0 commit comments

Comments
 (0)