Ejemplo n.º 1
0
def test_simulator_collapse_wavefunction(sim, mapper):
    engine_list = [LocalOptimizer()]
    if mapper is not None:
        engine_list.append(mapper)

    engine_list.append(GreedyScheduler())
    eng = HiQMainEngine(sim, engine_list=engine_list)
    qubits = eng.allocate_qureg(4)
    # unknown qubits: raises
    with pytest.raises(RuntimeError):
        eng.backend.collapse_wavefunction(qubits, [0] * 4)
    eng.flush()
    eng.backend.collapse_wavefunction(qubits, [0] * 4)
    assert pytest.approx(eng.backend.get_probability([0] * 4, qubits)) == 1.
    All(H) | qubits[1:]
    eng.flush()
    assert pytest.approx(eng.backend.get_probability([0] * 4, qubits)) == .125
    # impossible outcome: raises
    with pytest.raises(RuntimeError):
        eng.backend.collapse_wavefunction(qubits, [1] + [0] * 3)
    eng.backend.collapse_wavefunction(qubits[:-1], [0, 1, 0])
    probability = eng.backend.get_probability([0, 1, 0, 1], qubits)
    assert probability == pytest.approx(.5)

    # reinitialize qubits
    All(Measure) | qubits
    del qubits
    qubits = eng.allocate_qureg(4)

    H | qubits[0]
    CNOT | (qubits[0], qubits[1])
    eng.flush()
    eng.backend.collapse_wavefunction([qubits[0]], [1])
    probability = eng.backend.get_probability([1, 1], qubits[0:2])
    assert probability == pytest.approx(1.)
Ejemplo n.º 2
0
def test_simulator_probability(sim, mapper):
    engine_list = [LocalOptimizer()]
    if mapper is not None:
        engine_list.append(mapper)

    engine_list.append(GreedyScheduler())
    eng = HiQMainEngine(sim, engine_list=engine_list)
    qubits = eng.allocate_qureg(6)
    All(H) | qubits
    eng.flush()
    bits = [0, 0, 1, 0, 1, 0]
    for i in range(6):
        assert (eng.backend.get_probability(
            bits[:i], qubits[:i]) == pytest.approx(0.5**i))
    extra_qubit = eng.allocate_qubit()
    with pytest.raises(RuntimeError):
        eng.backend.get_probability([0], extra_qubit)
    del extra_qubit
    All(H) | qubits
    Ry(2 * math.acos(math.sqrt(0.3))) | qubits[0]
    eng.flush()
    assert eng.backend.get_probability([0], [qubits[0]]) == pytest.approx(0.3)
    Ry(2 * math.acos(math.sqrt(0.4))) | qubits[2]
    eng.flush()
    assert eng.backend.get_probability([0], [qubits[2]]) == pytest.approx(0.4)
    assert (eng.backend.get_probability([0, 0],
                                        qubits[:3:2]) == pytest.approx(0.12))
    assert (eng.backend.get_probability([0, 1],
                                        qubits[:3:2]) == pytest.approx(0.18))
    assert (eng.backend.get_probability([1, 0],
                                        qubits[:3:2]) == pytest.approx(0.28))
    All(Measure) | qubits
Ejemplo n.º 3
0
def test_simulator_functional_entangle(sim):
    eng = HiQMainEngine(sim, [GreedyScheduler()])
    qubits = eng.allocate_qureg(5)
    # entangle all qubits:
    H | qubits[0]
    for qb in qubits[1:]:
        CNOT | (qubits[0], qb)

    eng.flush()

    id2pos, vec = sim.cheat()

    # check the state vector:
    assert .5 == pytest.approx(abs(vec[0])**2)  # amplitudes 00000 and 11111
    assert .5 == pytest.approx(abs(
        vec[31])**2)  # are never moved even if qubit reordering made
    for i in range(1, 31):
        assert 0. == pytest.approx(abs(vec[i]))

    # unentangle all except the first 2
    for qb in qubits[2:]:
        CNOT | (qubits[0], qb)

    # entangle using Toffolis
    for qb in qubits[2:]:
        Toffoli | (qubits[0], qubits[1], qb)

    eng.flush()

    # check the state vector:
    id2pos, vec = sim.cheat()
    assert .5 == pytest.approx(abs(vec[0])**2)
    assert .5 == pytest.approx(abs(vec[31])**2)
    for i in range(1, 31):
        assert 0. == pytest.approx(abs(vec[i]))

    # uncompute using multi-controlled NOTs
    with Control(eng, qubits[0:-1]):
        X | qubits[-1]
    with Control(eng, qubits[0:-2]):
        X | qubits[-2]
    with Control(eng, qubits[0:-3]):
        X | qubits[-3]
    CNOT | (qubits[0], qubits[1])
    H | qubits[0]

    eng.flush()

    id2pos, vec = sim.cheat()

    # check the state vector:
    assert 1. == pytest.approx(abs(vec[0])**2)
    for i in range(1, 32):
        assert 0. == pytest.approx(abs(vec[i]))

    All(Measure) | qubits
Ejemplo n.º 4
0
def test_simulator_functional_measurement(sim):
    eng = HiQMainEngine(sim, [GreedyScheduler()])
    qubits = eng.allocate_qureg(5)
    # entangle all qubits:
    H | qubits[0]
    for qb in qubits[1:]:
        CNOT | (qubits[0], qb)

    All(Measure) | qubits

    bit_value_sum = sum([int(qubit) for qubit in qubits])
    assert bit_value_sum == 0 or bit_value_sum == 5
Ejemplo n.º 5
0
def test_simulator_kqubit_exception(sim):
    m1 = Rx(0.3).matrix
    m2 = Rx(0.8).matrix
    m3 = Ry(0.1).matrix
    m4 = Rz(0.9).matrix.dot(Ry(-0.1).matrix)
    m = numpy.kron(m4, numpy.kron(m3, numpy.kron(m2, m1)))

    class KQubitGate(BasicGate):
        @property
        def matrix(self):
            return m

    eng = HiQMainEngine(sim, [GreedyScheduler()])
    qureg = eng.allocate_qureg(3)
    with pytest.raises(Exception):
        KQubitGate() | qureg
        eng.flush()
    with pytest.raises(Exception):
        H | qureg
        eng.flush()
Ejemplo n.º 6
0
def test_simulator_amplitude(sim, mapper):
    engine_list = [LocalOptimizer()]
    if mapper is not None:
        engine_list.append(mapper)

    engine_list.append(GreedyScheduler())
    eng = HiQMainEngine(sim, engine_list=engine_list)
    qubits = eng.allocate_qureg(6)
    All(X) | qubits
    All(H) | qubits
    eng.flush()
    bits = [0, 0, 1, 0, 1, 0]
    assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(1. / 8.)
    bits = [0, 0, 0, 0, 1, 0]
    assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(-1. / 8.)
    bits = [0, 1, 1, 0, 1, 0]
    assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(-1. / 8.)
    All(H) | qubits
    All(X) | qubits
    Ry(2 * math.acos(0.3)) | qubits[0]
    eng.flush()
    bits = [0] * 6
    assert eng.backend.get_amplitude(bits, qubits) == pytest.approx(0.3)
    bits[0] = 1
    assert (eng.backend.get_amplitude(bits, qubits) == pytest.approx(
        math.sqrt(0.91)))
    All(Measure) | qubits
    # raises if not all qubits are in the list:
    with pytest.raises(RuntimeError):
        eng.backend.get_amplitude(bits, qubits[:-1])
    # doesn't just check for length:
    with pytest.raises(RuntimeError):
        eng.backend.get_amplitude(bits, qubits[:-1] + [qubits[0]])
    extra_qubit = eng.allocate_qubit()
    eng.flush()
    # there is a new qubit now!
    with pytest.raises(RuntimeError):
        eng.backend.get_amplitude(bits, qubits)
Ejemplo n.º 7
0
def test_simulator_kqubit_gate(sim):
    m1 = Rx(0.3).matrix
    m2 = Rx(0.8).matrix
    m3 = Ry(0.1).matrix
    m4 = Rz(0.9).matrix.dot(Ry(-0.1).matrix)
    m = numpy.kron(m4, numpy.kron(m3, numpy.kron(m2, m1)))

    class KQubitGate(BasicGate):
        @property
        def matrix(self):
            return m

    eng = HiQMainEngine(sim, [GreedyScheduler()])
    qureg = eng.allocate_qureg(4)
    qubit = eng.allocate_qubit()
    Rx(-0.3) | qureg[0]
    Rx(-0.8) | qureg[1]
    Ry(-0.1) | qureg[2]
    Rz(-0.9) | qureg[3]
    Ry(0.1) | qureg[3]
    X | qubit
    with Control(eng, qubit):
        KQubitGate() | qureg
    X | qubit
    with Control(eng, qubit):
        with Dagger(eng):
            KQubitGate() | qureg
    assert sim.get_amplitude('0' * 5, qubit + qureg) == pytest.approx(1.)

    class LargerGate(BasicGate):
        @property
        def matrix(self):
            return numpy.eye(2**6)

    with pytest.raises(Exception):
        LargerGate() | (qureg + qubit)
        eng.flush()
Ejemplo n.º 8
0
    rule_set = DecompositionRuleSet(modules=[projectq.setups.decompositions])
    engines = [
        TagRemover(),
        LocalOptimizer(cache_depth),
        AutoReplacer(rule_set),
        TagRemover(),
        LocalOptimizer(cache_depth),
        GreedyScheduler()
    ]

    eng = HiQMainEngine(backend, engines)

    if MPI.COMM_WORLD.Get_rank() == 0:

        #allocate all the qubits
        layer1_weight_reg = eng.allocate_qureg(6)
        layer1_input_reg = eng.allocate_qureg(6)

        layer2_weight_reg = eng.allocate_qureg(2)

        output_reg = eng.allocate_qureg(3)
        des_output = eng.allocate_qubit()
        ancilla_qubit = eng.allocate_qubit()
        ancilla2 = eng.allocate_qubit()
        phase_reg = eng.allocate_qureg(3)

        #initialize the ancilla and weight qubits
        X | ancilla_qubit
        H | ancilla_qubit

        All(H) | layer1_weight_reg
Ejemplo n.º 9
0
# Create main engine to compile the code to machine instructions(required)
#eng = HiQMainEngine(SimulatorMPI(gate_fusion=True, num_local_qubits=20))
eng = HiQMainEngine(engine_list=get_engine_list() + [CommandPrinter()])

# Qubit number N
num_of_qubit = 5

#Circuit Depth
depth = 30

# Gate
gate_set = [X, Y, Z, T, H, SqrtX, CNOT]

# Use the method provided by the main engine to create qubit registers
qureg = eng.allocate_qureg(num_of_qubit)

for i in range(depth):

    #Pick random gate from gate_set
    chosen_gate = random.sample(gate_set, 1)
    gate = chosen_gate[0]

    #Gate operation
    #If the chosen gate is a two qubit gate
    if gate in [CNOT]:

        #Choose two qubit from qureg
        l = random.sample(qureg, 2)

        # To determine the control qubit and target qubit
Ejemplo n.º 10
0
  		and run it on our backend. We will compare the final state your
  		circuit produces with our target state by ourselves.
  """
    simulated_circuit = 'H | qubit[0]; H | qubit[1]'
    return simulated_circuit


if __name__ == "__main__":
    # use projectq simulator
    #eng = MainEngine()
    # use hiq simulator

    backend = SimulatorMPI(gate_fusion=True)
    cache_depth = 10
    rule_set = DecompositionRuleSet(modules=[projectq.setups.decompositions])
    engines = [
        TagRemover(),
        LocalOptimizer(cache_depth),
        AutoReplacer(rule_set),
        TagRemover(),
        LocalOptimizer(cache_depth),
        GreedyScheduler()
    ]
    # make the compiler and run the circuit on the simulator backend
    eng = HiQMainEngine(backend, engines)
    qureg = eng.allocate_qureg(5)
    target_state = [0.5, 0.5, 0.5, 0.5]
    mapper = '3\n1,2\n2,3'
    circuit = circuit_generator(eng, target_state, mapper)
    All(Measure) | qureg
    print(circuit)
Ejemplo n.º 11
0
def run_qecc9():

    #init simulator
    simulator = StabilizerSimulator(12)
    eng = HiQMainEngine(simulator, [])

    #allocate
    qubits = eng.allocate_qureg(12)

    #start
    print("= Encoded the qubit, state is: |0>")
    #circuit
    CNOT | (qubits[0], qubits[3])
    CNOT | (qubits[3], qubits[0])

    CNOT | (qubits[3], qubits[6])
    CNOT | (qubits[3], qubits[9])
    H | qubits[3]
    CNOT | (qubits[3], qubits[4])
    CNOT | (qubits[3], qubits[5])
    H | qubits[6]
    CNOT | (qubits[6], qubits[7])
    CNOT | (qubits[6], qubits[8])
    H | qubits[9]
    CNOT | (qubits[9], qubits[10])
    CNOT | (qubits[9], qubits[11])

    H | qubits[3]
    H | qubits[4]
    H | qubits[5]
    H | qubits[6]
    H | qubits[7]
    H | qubits[8]
    H | qubits[9]
    H | qubits[10]
    H | qubits[11]
    CNOT | (qubits[1], qubits[3])
    CNOT | (qubits[1], qubits[4])
    CNOT | (qubits[1], qubits[5])
    CNOT | (qubits[1], qubits[6])
    CNOT | (qubits[1], qubits[7])
    CNOT | (qubits[1], qubits[8])
    CNOT | (qubits[1], qubits[9])
    CNOT | (qubits[1], qubits[10])
    CNOT | (qubits[1], qubits[11])
    H | qubits[3]
    H | qubits[4]
    H | qubits[5]
    H | qubits[6]
    H | qubits[7]
    H | qubits[8]
    H | qubits[9]
    H | qubits[10]
    H | qubits[11]

    CNOT | (qubits[2], qubits[3])
    CNOT | (qubits[2], qubits[4])
    CNOT | (qubits[2], qubits[5])
    CNOT | (qubits[2], qubits[6])
    CNOT | (qubits[2], qubits[7])
    CNOT | (qubits[2], qubits[8])
    CNOT | (qubits[2], qubits[9])
    CNOT | (qubits[2], qubits[10])
    CNOT | (qubits[2], qubits[11])

    CNOT | (qubits[9], qubits[11])
    CNOT | (qubits[9], qubits[10])
    H | qubits[9]
    CNOT | (qubits[6], qubits[8])
    CNOT | (qubits[6], qubits[7])
    H | qubits[6]
    CNOT | (qubits[3], qubits[5])
    CNOT | (qubits[3], qubits[4])
    H | qubits[3]
    CNOT | (qubits[3], qubits[9])
    CNOT | (qubits[3], qubits[6])

    Measure | qubits[3]
    #flush
    eng.flush()

    print("= Decoded the qubit, state is: |{}>".format(int(qubits[3])))
Ejemplo n.º 12
0
    cache_depth = 10
    rule_set = DecompositionRuleSet(modules=[projectq.setups.decompositions])
    engines = [
        TagRemover(),
        LocalOptimizer(cache_depth),
        AutoReplacer(rule_set),
        TagRemover(),
        LocalOptimizer(cache_depth),
        GreedyScheduler()
    ]

    eng = HiQMainEngine(backend, engines)

    if MPI.COMM_WORLD.Get_rank() == 0:

        layer1_weight_reg = eng.allocate_qureg(3)
        layer1_input_reg = eng.allocate_qureg(3)

        output = eng.allocate_qubit()
        des_output = eng.allocate_qubit()
        ancilla_qubit2 = eng.allocate_qubit()
        ancilla_qubit = eng.allocate_qubit()
        phase_reg = eng.allocate_qureg(3)

        X | ancilla_qubit
        H | ancilla_qubit

        All(H) | layer1_weight_reg

        #run the training cycle, one should adjust the number of loops and run the whole program again to get results for different iterations
        with Loop(eng, 1):
Ejemplo n.º 13
0
    ]

    eng = HiQMainEngine(backend, engines)
    m = 2
    epsilon = 0.1  # the estimation algorithm successs with probability 1 - epsilon
    n_accuracy = 3  # the estimation algorithm estimates with 3 bits accuracy
    n = _bits_required_to_achieve_accuracy(n_accuracy, epsilon)

    ## we create a unitary U = R(cmath.pi*3/4) \ox R(cmath.pi*3/4)
    U = BasicGate()
    theta = math.pi * 3 / 4
    U.matrix = np.matrix([[1, 0, 0, 0], [0, cmath.exp(1j * theta), 0, 0],
                          [0, 0, cmath.exp(1j * theta), 0],
                          [0, 0, 0, cmath.exp(1j * 2 * theta)]])

    state = eng.allocate_qureg(m + n)

    # prepare the input state to be |psi>=|01>
    X | state[1]

    run_phase_estimation(eng, U, state, m, n)

    # we have to post-process the state that stores the estimated phase
    OFFSET = m
    Tensor(Measure) | state[OFFSET:]
    Tensor(Measure) | state[:OFFSET]
    eng.flush()

    outcome = ""  # used to store the binary string of the estimated phase
    value = 0  # used to store the decimal value of the estimated phase
    for k in range(n_accuracy):
Ejemplo n.º 14
0
        raise NotMergeable


# Command.self_merge_multi = self_merge_multi
# LocalOptimizer._optimize = my_optimize
# LocalOptimizer.multigate_merge = multigate_merge
#################################


# create a list of restriction engines
restric_engine = restrictedgateset.get_engine_list(one_qubit_gates=(X,Y,Z,H,S,T,Rx,Ry,Rz),
                                                two_qubit_gates=(CZ,CX))

# eng = HiQMainEngine(backend, engine_list=[CommandPrinter()] + engines + [CommandPrinter()])
eng = HiQMainEngine(backend, engine_list= engines)
Qureg = eng.allocate_qureg(14)

theta1=0
theta2=0
theta3=0
theta4=0
theta5=0

from circuit import example_circuit

example_circuit(Qureg, theta1, theta2, theta3, theta4, theta5)

All(Measure) | Qureg