Example #1
0
def test_random_2():
    # import pudb
    # pudb.set_trace()
    qubits = [qu.create_qubit() for _ in range(3)]
    qubits[2] = gates.ygate(qubits[2])
    c, t = gates.controlled_ygate(qubits[2], qubits[0])
    qubits[2], qubits[0] = c, t
    out = qu.contract_network(qubits)
    result = qu.eval_probability(out).ravel()
    ref = np.array(
        [0 + 0j, 0 + 0j, 0 + 0j, 0 + 0j, 0 + 0j, -1 + 0j, 0 + 0j, 0 + 0j])
    np.testing.assert_allclose(result, ref)
Example #2
0
def test_random_circuits():
    # import pudb
    # pudb.set_trace()
    simulator = cirq.Simulator()
    for _ in range(1):
        ref = ""
        cirq_circuit = cirq.Circuit()
        num_qubits = random.randint(1, 6)
        num_gates = random.randint(0, 6)
        ref += "random circuit with " + str(num_qubits) + \
          " qubits and " + str(num_gates) + " gates"
        # qubits for each library
        quantn_qubits = [qu.create_qubit() for _ in range(num_qubits)]
        cirq_qubits = [cirq.GridQubit(0, j) for j in range(num_qubits)]
        # assemble random operations
        for i in range(num_gates):
            op = random.randint(0, len(ops) - 1)
            q_0 = random.randint(0, num_qubits - 1)
            # single qubit operation
            if ops[op][1] == 1:
                ref += "\nattaching gate: " + ops[op][0] + " to qubit " + str(
                    q_0)
                target = ops[op][3](quantn_qubits[q_0])
                quantn_qubits[q_0] = target
                cirq_circuit.append(ops[op][2](cirq_qubits[q_0]))
            # two qubit operation
            else:
                q_1 = None
                while q_1 == None or q_0 == q_1:
                    q_1 = random.randint(0, num_qubits - 1)
                ref += "\nattaching gate: " + ops[op][0] + " to qubits " + str(
                    q_0) + str(q_1)
                control, target = ops[op][3](quantn_qubits[q_0],
                                             quantn_qubits[q_1])
                quantn_qubits[q_0] = control
                quantn_qubits[q_1] = target
                cirq_circuit.append(ops[op][2](cirq_qubits[q_0],
                                               cirq_qubits[q_1]))
        # assemble cirq circuit
        cirq_result = simulator.simulate(cirq_circuit, qubit_order=cirq_qubits)
        # execute quantn circuit
        quantn_circuit = qu.contract_network(quantn_qubits)
        quantn_result = qu.eval_probability(quantn_circuit)
        # compare
        if not np.allclose(quantn_result, cirq_result.final_state):
            with fullprint():
                print(ref, "\nmine:\n", quantn_result, "ref:\n",
                      cirq_result.final_state)
        np.testing.assert_allclose(quantn_result, cirq_result.final_state)
Example #3
0
def test_eval_probability_returns_probability_amplitude():
  q_0 = qu.create_qubit()
  reference = np.array([1, 0])
  state = qu.eval_probability(q_0.node1)
  np.testing.assert_allclose(state, reference)
Example #4
0
def test_eval_probability_rejects_edges():
  q_0 = qu.create_qubit()
  with pytest.raises(ValueError):
    state = qu.eval_probability(q_0)
Example #5
0
def test_eval_probability_rejects_uncontracted():
  q_0 = qu.create_qubit()
  gate = gates.xgate(q_0)
  with pytest.raises(ValueError):
    state = qu.eval_probability(q_0)