0CTF 2019 Final - Quantum Game (Crypto, 214)

Challenge

Quantum computing is so interesting!
nc 192.168.201.24 13337

File : quantum_game.py


Solution

Stage1

for i in range(100):
stage1 = choice1 + rand_choice() + choice2
one_result = self.qc.run_and_measure(stage1, 1)[0]
result.append(one_result[0])
if all(result):
self.send(b'You win!')

In stage 1, Allow us to use two gates to rotate qbit.

def rand_choice():
if (os.urandom(1)[0] & 1):
return Program('X 0')
else:
return Program('I 0')

rand_choice will spin on X-axis.

In choice1, we can place Hadamard gate first.

Change initial state (+Z) to +X, then rand_choice will do nothing with it.

then we put Hadamard gate again in choice2, the state will go back to +Z

Finally, put X gate in the end to make +Z spin to -Z

The measurement will always be 1

Stage 2

your_choice = self.get_choice()
stage2 = choice1 + your_choice + choice2
result = self.qc.run_and_measure(stage2, 100)[0]

In stage 2, this challenge require you to defeat your solution in stage 1.

this challenge is: H + ? + H + X

In this case, spin Y-axis is well. H + Y + H + X will make everything go back to 0.

Stage 3

target = random.randint(1000, 99000)
self.send(f'level3 - coin master (get {target} heads in 100000 tosses)'.encode('ascii'))

# ...

if diff < 100:
self.send(b'You win!')

In stage 3, this time they require your to turn as many head coin as they are.

We must find out what the axis they use.

The probability that the state of the angle t with the X axis on the YZ plane is 1 is sin(t/2)^2.

we have known that the probability is target/100000 * 100.

so, we can reverse it : 2 * np.arcsin(np.sqrt(target / 100000))

flag{D0_yoU_eNj0y_f4nta5tIc_QuaNtum_9aMe?}

solve.py