5
$\begingroup$

A beginner question after watching few videos.

Say, var=a; var can be either of two values, a or b. Check what is the value of var, using Q#, QISKit or similar.

Any help/idea?

$\endgroup$

2 Answers 2

3
$\begingroup$

You can check for state equality with the SWAP test.

Quantum fingerprinting (Buhrman, Cleve, Watrous & de Wolf, 2001) seems to be the first paper to introduce the SWAP test.

The idea behind this test is:

  1. Encode the 2 quantum states using quantum error correction codes to "increase the difference between them".
  2. Test the 2 code words by using an ancilla register and the procedure below.
  3. Read the ancilla register. If it is $\left\vert 0 \right\rangle$ then the 2 states are probably equal. Else, they are probably different.

SWAP test

You can repeat the procedure multiple times to ensure that the 2 states are equal up to a given probability.

You can implement by yourself the test on QISKit:

from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, execute
from qiskit import IBMQ, BasicAer

q_simulator = BasicAer.get_backend('qasm_simulator')

register_size = 2

qr_psi = QuantumRegister(register_size, 'psi')  #For state PSI
qr_phi = QuantumRegister(register_size, 'phi')  #For state PHI
qr_ancilla = QuantumRegister(1, 'ancilla')
cequal = ClassicalRegister(1, 'equal')

circuit = QuantumCircuit()

circuit.add_register(qr_psi)
circuit.add_register(qr_phi)
circuit.add_register(qr_ancilla)
circuit.add_register(cequal)


def cswap(circuit, ctrl, q1, q2) -> None:
    assert(len(q1) == len(q2), "The swapped register sizes should match")
    for i in range(len(q1)):
        # Controlled swap
        circuit.ccx(ctrl, q1[i], q2[i])
        circuit.ccx(ctrl, q2[i], q1[i])
        circuit.ccx(ctrl, q1[i], q2[i])

def equality_test(circuit, ancilla, q1, q2, classical_register) -> None:
    assert(len(q1) == len(q2), "The swapped register sizes should match")
    circuit.h(ancilla[0])
    cswap(circuit, ancilla[0], q1, q2)
    circuit.h(ancilla[0])
    circuit.measure(ancilla[0], classical_register[0])

## Initialisation
# We add Hadamard to all the registers - to create PSI
circuit.h(qr_psi)
# We add Hadamard to all the registers - to create PHI
# Modify the initialisation of either PHI or PSI and check the results.
circuit.x(qr_phi)
circuit.h(qr_phi)

## SWAP test
equality_test(circuit, qr_ancilla, qr_psi, qr_phi, cequal)

res_qasm = execute([circuit], q_simulator, shots=1024).result()
counts = res_qasm.get_counts()

print(counts)

You can convince yourself that the method works with high probability by changing the initialisation step of the two registers and check the results when the 2 register match or don't match.

$\endgroup$
1
$\begingroup$

In Q# you use either the M or the Measure operations to measure a qubit.

Once you have the measurement of the Qubit, you can use things like if, for or other control flow statements to control your program execution based on the variable values.

Does that answer your question?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.