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?


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:

import qiskit

register_size = 2

    "name": "SWAP-test",
    "circuits": [
            "name": "simple_SWAP_test",
            "quantum_registers": [
                    "name": "psi",
                    "size": register_size
                },                {
                    "name": "phi",
                    "size": register_size
                    "name": "ancilla",
                    "size": 1
            "classical_registers": [
                    "name": "equal",
                    "size": 1
Q_program = qiskit.QuantumProgram(specs=Q_SPECS)

circuit = Q_program.get_circuit("simple_SWAP_test")
ancilla = Q_program.get_quantum_register("ancilla")
psi = Q_program.get_quantum_register("psi")
phi = Q_program.get_quantum_register("phi")
cequal = Q_program.get_classical_register('equal')

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")
    cswap(circuit, ancilla[0], q1, q2)
    circuit.measure(ancilla[0], classical_register[0])

## Initialisation
# Psi
# Phi

## SWAP test
equality_test(circuit, ancilla, psi, phi, cequal)

qasm_sim = qiskit.get_backend('local_qasm_simulator')
res_qasm = qiskit.execute([circuit], qasm_sim, shots=10**5).result()
counts = res_qasm.get_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.


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?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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