コード例 #1
0
def test_circuit_set_parameters_with_dictionary(backend, accelerators):
    """Check updating parameters of circuit with list."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    c = Circuit(3, accelerators)
    c.add(gates.X(0))
    c.add(gates.X(2))
    c.add(gates.ZPow(0, theta=0))
    c.add(gates.RZ(1, theta=0))
    c.add(gates.CZ(1, 2))
    c.add(gates.CZPow(0, 2, theta=0))
    c.add(gates.H(2))
    c.add(gates.Unitary(np.eye(2), 1))
    final_state = c()

    params = [0.123, 0.456, 0.789, np.random.random((2, 2))]
    target_c = Circuit(3, accelerators)
    target_c.add(gates.X(0))
    target_c.add(gates.X(2))
    target_c.add(gates.ZPow(0, theta=params[0]))
    target_c.add(gates.RZ(1, theta=params[1]))
    target_c.add(gates.CZ(1, 2))
    target_c.add(gates.CZPow(0, 2, theta=params[2]))
    target_c.add(gates.H(2))
    target_c.add(gates.Unitary(params[3], 1))

    param_dict = {c.queue[i]: p for i, p in zip([2, 3, 5, 7], params)}
    c.set_parameters(param_dict)
    np.testing.assert_allclose(c(), target_c())
    qibo.set_backend(original_backend)
コード例 #2
0
ファイル: test_gates.py プロジェクト: pat-phattharaporn/qibo
def test_zpow_gate(backend):
    """Check ZPow and CZPow gate fall back to U1 and CU1 respectively."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234

    c = Circuit(1)
    c.add(gates.X(0))
    c.add(gates.ZPow(0, theta))
    final_state = c.execute().numpy()
    target_state = np.zeros_like(final_state)
    target_state[1] = np.exp(1j * theta)
    np.testing.assert_allclose(final_state, target_state)
    assert c.queue[1].name == "u1"

    c = Circuit(2)
    c.add([gates.X(0), gates.X(1)])
    c.add(gates.CZPow(0, 1, theta))
    final_state = c.execute().numpy()
    target_state = np.zeros_like(final_state)
    target_state[-1] = np.exp(1j * theta)
    np.testing.assert_allclose(final_state, target_state)
    assert c.queue[2].name == "cu1"

    qibo.set_backend(original_backend)
コード例 #3
0
def test_zpow(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    final_state = apply_gates([gates.X(0), gates.ZPow(0, theta)], nqubits=1)
    target_state = np.zeros_like(final_state)
    target_state[1] = np.exp(1j * theta)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
コード例 #4
0
ファイル: test_cirq.py プロジェクト: hixio-mh/qibo
def test_zpow_gate(backend, nqubits, ndevices):
    """Check ZPow gate."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    targets = random_active_qubits(nqubits, nactive=1)
    qibo_gate = gates.ZPow(*targets, theta)
    cirq_gate = [(cirq.ZPowGate(exponent=theta / np.pi), targets)]
    assert_gates_equivalent(qibo_gate, cirq_gate, nqubits, ndevices)
    qibo.set_backend(original_backend)
コード例 #5
0
ファイル: test_gates.py プロジェクト: hixio-mh/qibo
def test_controlled_zpow(backend):
    """Check controlled ZPow and fallback to CZPow."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234

    c = Circuit(3)
    c.add(gates.X(0))
    c.add(gates.X(1))
    c.add(gates.X(2))
    c.add(gates.ZPow(2, theta).controlled_by(0, 1))
    c.add(gates.X(0))
    c.add(gates.X(1))
    final_state = c.execute().numpy()
    target_state = np.zeros_like(final_state)
    target_state[1] = np.exp(1j * theta)
    np.testing.assert_allclose(final_state, target_state)

    gate = gates.ZPow(0, theta).controlled_by(1)
    assert gate.__class__.__name__ == "CZPow"
    qibo.set_backend(original_backend)
コード例 #6
0
ファイル: test_gates.py プロジェクト: hixio-mh/qibo
def test_zpow(backend):
    """Check ZPow gate is working properly when qubit is on |1>."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234

    c = Circuit(1)
    c.add(gates.X(0))
    c.add(gates.ZPow(0, theta))
    final_state = c.execute().numpy()

    target_state = np.zeros_like(final_state)
    target_state[1] = np.exp(1j * theta )
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
コード例 #7
0
ファイル: test_gates.py プロジェクト: hixio-mh/qibo
def test_construct_unitary(backend):
    qibo.set_backend(backend)
    target_matrix = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
    np.testing.assert_allclose(gates.H(0).unitary, target_matrix)
    target_matrix = np.array([[0, 1], [1, 0]])
    np.testing.assert_allclose(gates.X(0).unitary, target_matrix)
    target_matrix = np.array([[0, -1j], [1j, 0]])
    np.testing.assert_allclose(gates.Y(0).unitary, target_matrix)
    target_matrix = np.array([[1, 0], [0, -1]])
    np.testing.assert_allclose(gates.Z(1).unitary, target_matrix)

    target_matrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                              [0, 0, 0, 1], [0, 0, 1, 0]])
    np.testing.assert_allclose(gates.CNOT(0, 1).unitary, target_matrix)
    target_matrix = np.diag([1, 1, 1, -1])
    np.testing.assert_allclose(gates.CZ(1, 3).unitary, target_matrix)
    target_matrix = np.array([[1, 0, 0, 0], [0, 0, 1, 0],
                              [0, 1, 0, 0], [0, 0, 0, 1]])
    np.testing.assert_allclose(gates.SWAP(2, 4).unitary, target_matrix)
    target_matrix = np.array([[1, 0, 0, 0, 0, 0, 0, 0],
                              [0, 1, 0, 0, 0, 0, 0, 0],
                              [0, 0, 1, 0, 0, 0, 0, 0],
                              [0, 0, 0, 1, 0, 0, 0, 0],
                              [0, 0, 0, 0, 1, 0, 0, 0],
                              [0, 0, 0, 0, 0, 1, 0, 0],
                              [0, 0, 0, 0, 0, 0, 0, 1],
                              [0, 0, 0, 0, 0, 0, 1, 0]])
    np.testing.assert_allclose(gates.TOFFOLI(1, 2, 3).unitary, target_matrix)

    theta = 0.1234
    target_matrix = np.array([[np.cos(theta / 2.0), -1j * np.sin(theta / 2.0)],
                              [-1j * np.sin(theta / 2.0), np.cos(theta / 2.0)]])
    np.testing.assert_allclose(gates.RX(0, theta).unitary, target_matrix)
    target_matrix = np.array([[np.cos(theta / 2.0), -np.sin(theta / 2.0)],
                              [np.sin(theta / 2.0), np.cos(theta / 2.0)]])
    np.testing.assert_allclose(gates.RY(0, theta).unitary, target_matrix)
    target_matrix = np.diag([np.exp(-1j * theta / 2.0), np.exp(1j * theta / 2.0)])
    np.testing.assert_allclose(gates.RZ(0, theta).unitary, target_matrix)
    target_matrix = np.diag([1, np.exp(1j * theta)])
    np.testing.assert_allclose(gates.ZPow(0, theta).unitary, target_matrix)
    target_matrix = np.diag([1, 1, 1, np.exp(1j * theta)])
    np.testing.assert_allclose(gates.CZPow(0, 1, theta).unitary, target_matrix)
    from qibo import matrices
    target_matrix = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0],
                              [0, 0, 0, 1]])
    np.testing.assert_allclose(matrices.SWAP, target_matrix)