Пример #1
0
def _tk1_to_x_v_rz(a: float, b: float, c: float) -> Circuit:
    circ = Circuit(1)
    if _approx_0_mod_2(b):
        circ.Rz(a + c, 0)
    elif _approx_0_mod_2(b + 1):
        if _approx_0_mod_2(a - 0.5) and _approx_0_mod_2(c - 0.5):
            circ.X(0)
        else:
            circ.Rz(c, 0).X(0).Rz(a, 0)
    else:
        if _approx_0_mod_2(a - 0.5) and _approx_0_mod_2(c - 0.5):
            circ.V(0).Rz(1 - b, 0).V(0)
        else:
            circ.Rz(c + 0.5, 0).V(0).Rz(b - 1, 0).V(0).Rz(a + 0.5, 0)
    return circ
Пример #2
0
def test_honeywell() -> None:
    token = os.getenv("HQS_AUTH")
    backend = HoneywellBackend(
        device_name="HQS-LT-1.0-APIVAL", machine_debug=skip_remote_tests
    )
    c = Circuit(4, 4, "test 1")
    c.H(0)
    c.CX(0, 1)
    c.Rz(0.3, 2)
    c.CSWAP(0, 1, 2)
    c.CRz(0.4, 2, 3)
    c.CY(1, 3)
    c.ZZPhase(0.1, 2, 0)
    c.Tdg(3)
    c.measure_all()
    backend.compile_circuit(c)
    n_shots = 4
    handle = backend.process_circuits([c], n_shots)[0]
    correct_shots = np.zeros((4, 4))
    correct_counts = {(0, 0, 0, 0): 4}
    res = backend.get_result(handle, timeout=49)
    shots = res.get_shots()
    counts = res.get_counts()
    assert backend.circuit_status(handle).status is StatusEnum.COMPLETED
    assert np.all(shots == correct_shots)
    assert counts == correct_counts
    newshots = backend.get_shots(c, 4, timeout=49)
    assert np.all(newshots == correct_shots)
    newcounts = backend.get_counts(c, 4)
    assert newcounts == correct_counts
    if token is None:
        assert backend.device is None
Пример #3
0
def test_estimates() -> None:
    """
    Check that the resource estimator gives reasonable results.
    """
    b = QsharpEstimatorBackend()
    c = Circuit(3)
    c.H(0)
    c.CX(0, 1)
    c.CCX(0, 1, 2)
    c.Rx(0.3, 1)
    c.Ry(0.4, 2)
    c.Rz(1.1, 0)
    c.S(1)
    c.SWAP(0, 2)
    c.T(1)
    c.X(0)
    c.Y(1)
    c.Z(2)
    pbox = PauliExpBox([Pauli.X, Pauli.I, Pauli.Z], 0.25)
    c.add_pauliexpbox(pbox, [2, 0, 1])
    b.compile_circuit(c, 0)
    resources = b.get_resources(c)
    assert resources["CNOT"] >= 1
    assert resources["QubitClifford"] >= 1
    assert resources["R"] >= 1
    assert resources["T"] >= 1
    assert resources["Depth"] >= 1
    assert resources["Width"] == 3
    assert resources["BorrowedWidth"] == 0
def test_pauli() -> None:
    c = Circuit(2)
    c.Rz(0.5, 0)
    b = ProjectQBackend()
    zi = QubitPauliString({Qubit(0): Pauli.Z})
    assert np.isclose(get_pauli_expectation_value(c, zi, b), complex(1))
    c.X(0)
    assert np.isclose(get_pauli_expectation_value(c, zi, b), complex(-1))
Пример #5
0
def test_pauli_statevector(qvm: None, quilc: None) -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    b = ForestStateBackend()
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    assert get_pauli_expectation_value(c, zi, b) == 1
    c.X(0)
    assert get_pauli_expectation_value(c, zi, b) == -1
def h2_4q_circ(theta: float) -> Circuit:
    circ = Circuit(4).X(0).X(1)
    circ.Rx(0.5, 0).H(1).H(2).H(3)
    circ.CX(0, 1).CX(1, 2).CX(2, 3)
    circ.Rz((-2 / np.pi) * theta, 3)
    circ.CX(2, 3).CX(1, 2).CX(0, 1)
    circ.Rx(-0.5, 0).H(1).H(2).H(3)
    return circ
def h2_2q_circ(theta: float) -> Circuit:
    circ = Circuit(2).X(0)
    circ.Rx(0.5, 0).H(1)
    circ.CX(0, 1)
    circ.Rz((-2 / np.pi) * theta, 1)
    circ.CX(0, 1)
    circ.Rx(-0.5, 0).H(1)
    return circ
Пример #8
0
def test_clifford_compilation() -> None:
    b = CirqCliffordSimBackend()
    c = Circuit(3).H(0).X(1).Y(2).CX(0, 1).CX(2, 1).H(0).X(0).H(1).Z(2)
    b.compile_circuit(c)
    assert b.valid_circuit(c)
    c.Rz(0.3, 0)
    b.compile_circuit(c)
    assert not b.valid_circuit(c)
Пример #9
0
def _tk1_to_x_sx_rz(a: float, b: float, c: float) -> Circuit:
    circ = Circuit(1)
    if _approx_0_mod_2(b):
        circ.Rz(a + c, 0)
    elif _approx_0_mod_2(b + 1):
        if _approx_0_mod_2(a - 0.5) and _approx_0_mod_2(c - 0.5):
            circ.X(0)
        else:
            circ.Rz(c, 0).X(0).Rz(a, 0)
    else:
        # use SX; SX = e^{i\pi/4}V; V = RX(1/2)
        if _approx_0_mod_2(a - 0.5) and _approx_0_mod_2(c - 0.5):
            circ.SX(0).Rz(1 - b, 0).SX(0)
            circ.add_phase(-0.5)
        else:
            circ.Rz(c + 0.5, 0).SX(0).Rz(b - 1, 0).SX(0).Rz(a + 0.5, 0)
            circ.add_phase(-0.5)
    return circ
Пример #10
0
def test_pauli() -> None:
    for b in [AerBackend(), AerStateBackend()]:
        c = Circuit(2)
        c.Rz(0.5, 0)
        b.compile_circuit(c)
        zi = QubitPauliString(Qubit(0), Pauli.Z)
        assert cmath.isclose(get_pauli_expectation_value(c, zi, b), 1)
        c.X(0)
        assert cmath.isclose(get_pauli_expectation_value(c, zi, b), -1)
Пример #11
0
def test_pauli_statevector() -> None:
    c = Circuit(2)
    c.Rz(0.5, 0)
    Transform.OptimisePostRouting().apply(c)
    b = AerStateBackend()
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    assert get_pauli_expectation_value(c, zi, b) == 1
    c.X(0)
    assert get_pauli_expectation_value(c, zi, b) == -1
Пример #12
0
def test_operator_statevector(qvm: None, quilc: None) -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    b = ForestStateBackend()
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    iz = QubitPauliString(Qubit(1), Pauli.Z)
    op = QubitPauliOperator({zi: 0.3, iz: -0.1})
    assert get_operator_expectation_value(c, op, b) == pytest.approx(0.2)
    c.X(0)
    assert get_operator_expectation_value(c, op, b) == pytest.approx(-0.4)
Пример #13
0
def test_pauli_sim(qvm: None, quilc: None) -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    b = ForestBackend("9q-square")
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    energy = get_pauli_expectation_value(c, zi, b, 10)
    assert abs(energy - 1) < 0.001
    c.X(0)
    energy = get_pauli_expectation_value(c, zi, b, 10)
    assert abs(energy + 1) < 0.001
Пример #14
0
def test_pauli_sim() -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    Transform.OptimisePostRouting().apply(c)
    b = AerBackend()
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    energy = get_pauli_expectation_value(c, zi, b, 8000)
    assert abs(energy - 1) < 0.001
    c.X(0)
    energy = get_pauli_expectation_value(c, zi, b, 8000)
    assert abs(energy + 1) < 0.001
def construct_oracle(graph):
    """Generate the initial, unrouted circuit with the Rz gates based on electrostatics, given a networkx graph

    Parameters
    ----------
    mol_graph : networkx graph
        Networkx graph with nodes:atoms and edges:bonds

    Returns
    -------
    pytket Circuit
        simple pytket circuit with a qubit for each atom

    Notes
    -----
    The qubits are labelled with integers that match the integers found in
    the inputted networkx graph node labels"""
    # - generate list of edges along with list of inverted edges
    # - combine the inverted edges with non-inverted ones

    edges = list(graph.edges)
    inv_edges = [(edge[1], edge[0]) for edge in edges]
    combinededges = [val for pair in zip(edges, inv_edges) for val in pair]

    # Create circuit and unpack each edge, applying it as a our new circuit
    constraint_test_circuit = Circuit(graph.number_of_nodes())
    for qubit in range(graph.number_of_nodes()):
        constraint_test_circuit.H(qubit)

    # Go through every node and add the Rz gate that tries to represent electronic differences between the different atoms
    for index in range(0, graph.number_of_nodes()):
        if (graph.nodes[index]['symbol'] == 'O'):
            constraint_test_circuit.Rz(6*(3.14)/2, index)
        if (graph.nodes[index]['symbol'] == 'H'):
            constraint_test_circuit.Rz(1*(3.14)/2, index)
        if (graph.nodes[index]['symbol'] == 'N'):
            constraint_test_circuit.Rz(5*(3.14)/2, index)
        if (graph.nodes[index]['symbol'] == 'C'):
            constraint_test_circuit.Rz(4*(3.14)/2, index)

    return constraint_test_circuit
Пример #16
0
def test_rotations() -> None:
    """
    Check that Rz(0.5) acts as the identity.
    """
    b = QsharpSimulatorBackend()
    c = Circuit(1)
    c.Rz(0.5, 0)
    c.measure_all()
    b.compile_circuit(c)
    n_shots = 10
    shots = b.get_shots(c, n_shots)
    assert all(shots[i, 0] == 0 for i in range(n_shots))
Пример #17
0
def test_operator_sim(qvm: None, quilc: None) -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    b = ForestBackend("9q-square")
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    iz = QubitPauliString(Qubit(1), Pauli.Z)
    op = QubitPauliOperator({zi: 0.3, iz: -0.1})
    assert get_operator_expectation_value(c, op, b,
                                          10) == pytest.approx(0.2, rel=0.001)
    c.X(0)
    assert get_operator_expectation_value(c, op, b,
                                          10) == pytest.approx(-0.4, rel=0.001)
Пример #18
0
def test_convert_symbolic() -> None:
    c = Circuit(2)
    alpha = Symbol("alpha")
    c.Rx(alpha, 0)
    beta = fresh_symbol("alpha")
    c.Rz(beta * 2, 1)
    with pytest.raises(RuntimeError):
        qs = tk_to_qsharp(c)
    s_map = {alpha: 0.5, beta: 3.2}
    c.symbol_substitution(s_map)
    qs = tk_to_qsharp(c)
    assert "Rx" in qs
Пример #19
0
def test_expectation() -> None:
    b = BraketBackend(local=True)
    assert b.supports_expectation
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    iz = QubitPauliString(Qubit(1), Pauli.Z)
    op = QubitPauliOperator({zi: 0.3, iz: -0.1})
    assert get_pauli_expectation_value(c, zi, b) == 1
    assert get_operator_expectation_value(c, op, b) == pytest.approx(0.2)
    c.X(0)
    assert get_pauli_expectation_value(c, zi, b) == -1
    assert get_operator_expectation_value(c, op, b) == pytest.approx(-0.4)
Пример #20
0
def test_compilation_correctness() -> None:
    c = Circuit(5)
    c.H(0).H(1).H(2)
    c.CX(0, 1).CX(1, 2)
    c.Rx(0.25, 1).Ry(0.75, 1).Rz(0.5, 2)
    c.CCX(2, 1, 0)
    c.CY(1, 0).CY(2, 1)
    c.H(0).H(1).H(2)
    c.Rz(0.125, 0)
    c.X(1)
    c.Rz(0.125, 2).X(2).Rz(0.25, 2)
    c.SX(3).Rz(0.125, 3).SX(3)
    c.CX(0, 3).CX(0, 4)
    u_backend = AerUnitaryBackend()
    u = u_backend.get_unitary(c)
    ibm_backend = IBMQBackend("ibmq_santiago",
                              hub="ibm-q",
                              group="open",
                              project="main")
    for ol in range(3):
        p = ibm_backend.default_compilation_pass(optimisation_level=ol)
        cu = CompilationUnit(c)
        p.apply(cu)
        c1 = cu.circuit
        compiled_u = u_backend.get_unitary(c1)

        # Adjust for placement
        imap = cu.initial_map
        fmap = cu.final_map
        c_idx = {c.qubits[i]: i for i in range(5)}
        c1_idx = {c1.qubits[i]: i for i in range(5)}
        ini = {c_idx[qb]: c1_idx[node] for qb, node in imap.items()}
        inv_fin = {c1_idx[node]: c_idx[qb] for qb, node in fmap.items()}
        m_ini = lift_perm(ini)
        m_inv_fin = lift_perm(inv_fin)

        assert compare_unitaries(u, m_inv_fin @ compiled_u @ m_ini)
Пример #21
0
def test_aqt() -> None:
    # Run a circuit on the noisy simulator.
    token = cast(str, os.getenv("AQT_AUTH"))
    b = AQTBackend(token, device_name="sim/noise-model-1", label="test 1")
    c = Circuit(4, 4)
    c.H(0)
    c.CX(0, 1)
    c.Rz(0.3, 2)
    c.CSWAP(0, 1, 2)
    c.CRz(0.4, 2, 3)
    c.CY(1, 3)
    c.add_barrier([0, 1])
    c.ZZPhase(0.1, 2, 0)
    c.Tdg(3)
    c.measure_all()
    b.compile_circuit(c)
    n_shots = 10
    shots = b.get_shots(c, n_shots, seed=1, timeout=30)
    counts = b.get_counts(c, n_shots)
    assert len(shots) == n_shots
    assert sum(counts.values()) == n_shots
def test_statevector() -> None:
    b = AerStateBackend()
    circ = Circuit(3, name="test")
    circ.H(2)
    circ.X(0)
    circ.H(0)
    circ.CX(0, 1)
    circ.CZ(1, 2)
    circ.Sdg(0)
    circ.Tdg(1)
    circ.Z(1)
    circ.T(2)
    circ.Rx(0.3333, 1)
    circ.Rz(0.3333, 1)
    zxcirc = tk_to_pyzx(circ)
    assert zxcirc.name == circ.name
    b.compile_circuit(circ)
    state = b.get_state(circ)
    circ2 = pyzx_to_tk(zxcirc)
    assert circ2.name == circ.name
    b.compile_circuit(circ2)
    state2 = b.get_state(circ2)
    assert np.allclose(state, state2, atol=1e-10)
Пример #23
0
# We can also add a new register of qubits in one go:

c.add_q_register("delta", 4)
print(c.qubits)

# Similar commands are available for classical bits.
#
# We can add gates to the circuit as follows:

c.CX(0, 1)

# This command appends a CX gate with control `q[0]` and target `q[1]`. Note that the integer arguments are automatically converted to the default unit IDs. For simple circuits it is often easiest to stick to the default register and refer to the qubits by integers. To add gates to our own named units, we simply pass the `Qubit` (or classical `Bit`) as an argument. (We can't mix the two conventions in one command, however.)

c.H(new_q1)
c.CX(Qubit("q", 1), new_q2)
c.Rz(0.5, new_q2)
print(c.get_commands())

# Let's have a look at our circuit:

print(c.get_commands())

# ## Exporting to and importing from standard formats

# We can export a `Circuit` to a file in QASM format. Conversely, if we have such a file we can import it into `pytket`. There are some limitations on the circuits that can be converted: for example, multi-dimensional indices (as in `beta` and `gamma` above) are not allowed.
#
# Here is a simple example:

from pytket.qasm import circuit_to_qasm, circuit_from_qasm

c = Circuit(3, 1)
Пример #24
0
# Now, we can construct a circuit containing symbols. You can ask for symbols by calling the `fresh_symbol` method with a string as an argument. This string represents the preferred symbol name; if this has already been used elsewhere, an appropriate suffix of the form `_x`, with `x` a natural number, will be added to generate a new symbol, as shown below:

a = fresh_symbol("a")
a1 = fresh_symbol("a")
print(a)
print(a1)

# We are going to make a circuit using just three 'phase gadgets': `Rz` gates surrounded by ladders of `CX` gates.

b = fresh_symbol("b")
circ = Circuit(4)
circ.CX(0, 1)
circ.CX(1, 2)
circ.CX(2, 3)
circ.Rz(a, 3)
circ.CX(2, 3)
circ.CX(1, 2)
circ.CX(0, 1)
circ.CX(3, 2)
circ.CX(2, 1)
circ.CX(1, 0)
circ.Rz(b, 0)
circ.CX(1, 0)
circ.CX(2, 1)
circ.CX(3, 2)
circ.CX(0, 1)
circ.CX(1, 2)
circ.CX(2, 3)
circ.Rz(0.5, 3)
circ.CX(2, 3)
Пример #25
0
def _from_tk1(a: float, b: float, c: float) -> Circuit:
    circ = Circuit(1)
    circ.Rz(c, 0)
    circ.Rx(b, 0)
    circ.Rz(a, 0)
    return circ
Пример #26
0
def _tk1_to_phasedxrz(a: float, b: float, c: float) -> Circuit:
    circ = Circuit(1)
    circ.Rz(a + c, 0)
    circ.add_gate(OpType.PhasedX, [b, a], [0])
    RemoveRedundancies().apply(circ)
    return circ