示例#1
0
文件: test_cirq.py 项目: NourO93/qibo
def test_one_qubit_parametrized_gates(backend, gate_name, nqubits, ndevices):
    """Check parametrized one-qubit rotations."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    targets = random_active_qubits(nqubits, nactive=1)
    qibo_gate = getattr(gates, gate_name)(*targets, theta)
    cirq_gate = [(getattr(cirq, gate_name.lower())(theta), targets)]
    assert_gates_equivalent(qibo_gate, cirq_gate, nqubits, ndevices)
    qibo.set_backend(original_backend)
示例#2
0
def test_generalized_fsim_error(backend):
    """Check GenerelizedfSim gate raises error for wrong unitary shape."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    phi = np.random.random()
    rotation = utils.random_numpy_complex((4, 4))
    c = Circuit(2)
    with pytest.raises(ValueError):
        c.add(gates.GeneralizedfSim(0, 1, rotation, phi))
    qibo.set_backend(original_backend)
示例#3
0
文件: test_cirq.py 项目: NourO93/qibo
def test_u1_gate(backend, nqubits, ndevices):
    """Check U1 gate."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    targets = random_active_qubits(nqubits, nactive=1)
    qibo_gate = gates.U1(*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)
示例#4
0
文件: test_cirq.py 项目: NourO93/qibo
def test_qft(backend, accelerators, nqubits):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = models.QFT(nqubits, accelerators=accelerators)
    initial_state = random_state(nqubits)
    final_state = c(np.copy(initial_state))
    cirq_gates = [(cirq.qft, list(range(nqubits)))]
    target_state, _ = execute_cirq(cirq_gates, nqubits, np.copy(initial_state))
    np.testing.assert_allclose(target_state, final_state, atol=1e-6)
    qibo.set_backend(original_backend)
示例#5
0
def test_tensorflow_channel_errors():
    import tensorflow as tf
    original_backend = qibo.get_backend()
    qibo.set_backend("matmuleinsum")
    gate = gates.NoiseChannel(0, 0.1, 0.2, 0.3)
    state = tf.cast(np.random.random(4 * (2, )), dtype=tf.complex128)
    with pytest.raises(ValueError):
        state = gate(state, is_density_matrix=False)
    state = gate(state)
    qibo.set_backend(original_backend)
示例#6
0
def test_measurement_circuit(backend, accelerators):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = models.Circuit(4, accelerators)
    c.add(gates.X(0))
    c.add(gates.M(0))
    result = c(nshots=100)
    assert_result(result, np.ones((100, )), np.ones((100, 1)), {1: 100},
                  {"1": 100})
    qibo.set_backend(original_backend)
示例#7
0
def test_czpow(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    gatelist = [gates.X(0), gates.X(1), gates.CZPow(0, 1, theta)]
    final_state = apply_gates(gatelist, nqubits=2)
    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)
示例#8
0
def test_measurementresult_outcome(backend):
    import collections
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    result = measurements.MeasurementResult((0, ))
    result.decimal = np.zeros(1, dtype=np.int64)
    assert result.outcome() == 0
    result.decimal = np.ones(1, dtype=np.int64)
    assert result.outcome() == 1
    qibo.set_backend(original_backend)
示例#9
0
def test_entropy_product_state(backend):
    """Check that the |++> state has zero entropy."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    entropy = callbacks.EntanglementEntropy()
    state = np.ones(4) / 2.0

    result = entropy(state)
    np.testing.assert_allclose(result, 0, atol=_atol)
    qibo.set_backend(original_backend)
示例#10
0
def test_adding_gate_with_bad_nqubits(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    gate = gates.H(0)
    gate.nqubits = 5
    gate.prepare()
    c = Circuit(2)
    with pytest.raises(RuntimeError):
        c.add(gate)
    qibo.set_backend(original_backend)
示例#11
0
def test_thermal_relaxation_channel_errors(backend, t1, t2, time, excpop):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    with pytest.raises(ValueError):
        gate = gates.ThermalRelaxationChannel(0,
                                              t1,
                                              t2,
                                              time,
                                              excited_population=excpop)
    qibo.set_backend(original_backend)
示例#12
0
def test_controlled_dagger(backend, gate, args):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    gate = getattr(gates, gate)(*args).controlled_by(0, 1, 2)
    c = Circuit(4)
    c.add((gate, gate.dagger()))
    initial_state = random_state(4)
    final_state = c(np.copy(initial_state))
    np.testing.assert_allclose(final_state, initial_state)
    qibo.set_backend(original_backend)
示例#13
0
def test_state_shape_and_dtype(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    state = states.VectorState.zero_state(3)
    assert state.shape == (8, )
    assert state.dtype == K.dtypes('DTYPECPX')
    state = states.MatrixState.zero_state(3)
    assert state.shape == (8, 8)
    assert state.dtype == K.dtypes('DTYPECPX')
    qibo.set_backend(original_backend)
示例#14
0
def test_plus_state_initialization(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    state = states.VectorState.plus_state(4)
    target_state = np.ones(16) / 4
    np.testing.assert_allclose(state.tensor, target_state)
    state = states.MatrixState.plus_state(3)
    target_state = np.ones((8, 8)) / 8
    np.testing.assert_allclose(state.tensor, target_state)
    qibo.set_backend(original_backend)
示例#15
0
def test_compiling_twice_exception():
    """Check that compiling a circuit a second time raises error."""
    original_backend = qibo.get_backend()
    qibo.set_backend("matmuleinsum")
    c = Circuit(2)
    c.add([gates.H(0), gates.H(1)])
    c.compile()
    with pytest.raises(RuntimeError):
        c.compile()
    qibo.set_backend(original_backend)
示例#16
0
def test_circuit_set_parameters_ungates(backend, accelerators, trainable):
    """Check updating parameters of circuit with list."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    params = [0.1, 0.2, 0.3, (0.4, 0.5), (0.6, 0.7, 0.8)]
    if trainable:
        trainable_params = list(params)
    else:
        trainable_params = [0.1, 0.3, (0.4, 0.5)]

    c = Circuit(3, accelerators)
    c.add(gates.RX(0, theta=0))
    if trainable:
        c.add(gates.CRY(0, 1, theta=0, trainable=trainable))
    else:
        c.add(gates.CRY(0, 1, theta=params[1], trainable=trainable))
    c.add(gates.CZ(1, 2))
    c.add(gates.U1(2, theta=0))
    c.add(gates.CU2(0, 2, phi=0, lam=0))
    if trainable:
        c.add(gates.U3(1, theta=0, phi=0, lam=0, trainable=trainable))
    else:
        c.add(gates.U3(1, *params[4], trainable=trainable))
    # execute once
    final_state = c()

    target_c = Circuit(3)
    target_c.add(gates.RX(0, theta=params[0]))
    target_c.add(gates.CRY(0, 1, theta=params[1]))
    target_c.add(gates.CZ(1, 2))
    target_c.add(gates.U1(2, theta=params[2]))
    target_c.add(gates.CU2(0, 2, *params[3]))
    target_c.add(gates.U3(1, *params[4]))
    c.set_parameters(trainable_params)
    np.testing.assert_allclose(c(), target_c())

    # Attempt using a flat list
    npparams = np.random.random(8)
    if trainable:
        trainable_params = np.copy(npparams)
    else:
        npparams[1] = params[1]
        npparams[5:] = params[4]
        trainable_params = np.delete(npparams, [1, 5, 6, 7])
    target_c = Circuit(3)
    target_c.add(gates.RX(0, theta=npparams[0]))
    target_c.add(gates.CRY(0, 1, theta=npparams[1]))
    target_c.add(gates.CZ(1, 2))
    target_c.add(gates.U1(2, theta=npparams[2]))
    target_c.add(gates.CU2(0, 2, *npparams[3:5]))
    target_c.add(gates.U3(1, *npparams[5:]))
    c.set_parameters(trainable_params)
    np.testing.assert_allclose(c(), target_c())
    qibo.set_backend(original_backend)
示例#17
0
def test_one_qubit_gates_controlled_by(backend, gate_name, nqubits, ndevices):
    """Check one-qubit gates controlled on arbitrary number of qubits."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    all_qubits = np.arange(nqubits)
    for _ in range(5):
        activeq = random_active_qubits(nqubits, nmin=1)
        qibo_gate = getattr(gates, gate_name)(activeq[-1]).controlled_by(*activeq[:-1])
        cirq_gate = [(getattr(cirq, gate_name).controlled(len(activeq) - 1), activeq)]
        assert_gates_equivalent(qibo_gate, cirq_gate, nqubits, ndevices)
    qibo.set_backend(original_backend)
示例#18
0
def test_ygate(backend):
    """Check Y gate is working properly."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = Circuit(2)
    c.add(gates.Y(1))
    final_state = c.execute().numpy()
    target_state = np.zeros_like(final_state)
    target_state[1] = 1j
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#19
0
def test_construct_unitary_errors(backend):
    qibo.set_backend(backend)
    gate = gates.M(0)
    with pytest.raises(ValueError):
        matrix = gate.unitary

    pairs = list((i, i + 1) for i in range(0, 5, 2))
    theta = 2 * np.pi * np.random.random(6)
    gate = gates.VariationalLayer(range(6), pairs, gates.RY, gates.CZ, theta)
    with pytest.raises(ValueError):
        matrix = gate.unitary
示例#20
0
def test_xgate(backend, accelerators):
    """Check X gate is working properly."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = Circuit(2, accelerators)
    c.add(gates.X(0))
    final_state = c.execute().numpy()
    target_state = np.zeros_like(final_state)
    target_state[2] = 1.0
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#21
0
def test_unitary_bad_shape(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    matrix = np.random.random((8, 8))
    with pytest.raises(ValueError):
        gate = gates.Unitary(matrix, 0, 1)

    if backend == "custom":
        with pytest.raises(NotImplementedError):
            gate = gates.Unitary(matrix, 0, 1, 2)
    qibo.set_backend(original_backend)
示例#22
0
def test_cnot_no_effect(backend):
    """Check CNOT gate is working properly on |00>."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = Circuit(2)
    c.add(gates.CNOT(0, 1))
    final_state = c.execute().numpy()
    target_state = np.zeros_like(final_state)
    target_state[0] = 1.0
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#23
0
def test_hadamard(backend):
    """Check Hadamard gate is working properly."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = Circuit(2)
    c.add(gates.H(0))
    c.add(gates.H(1))
    final_state = c.execute().numpy()
    target_state = np.ones_like(final_state) / 2
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#24
0
def test_identity(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    gatelist = [gates.H(0), gates.H(1), gates.I(0), gates.I(1)]
    final_state = apply_gates(gatelist, nqubits=2)
    target_state = np.ones_like(final_state) / 2.0
    np.testing.assert_allclose(final_state, target_state)
    gatelist = [gates.H(0), gates.H(1), gates.I(0, 1)]
    final_state = apply_gates(gatelist, nqubits=2)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#25
0
def test_repeated_execute(backend, accelerators):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = Circuit(4, accelerators)
    thetas = np.random.random(4)
    c.add((gates.RY(i, t) for i, t in enumerate(thetas)))
    c.repeated_execution = True
    target_state = np.array(20 * [c()])
    final_state = c(nshots=20)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#26
0
def test_unitary(backend, nqubits):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    initial_state = np.ones(2 ** nqubits) / np.sqrt(2 ** nqubits)
    matrix = np.random.random(2 * (2 ** (nqubits - 1),))
    target_state = np.kron(np.eye(2), matrix).dot(initial_state)
    gatelist = [gates.H(i) for i in range(nqubits)]
    gatelist.append(gates.Unitary(matrix, *range(1, nqubits), name="random"))
    final_state = apply_gates(gatelist, nqubits=nqubits)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#27
0
def test_ry(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    final_state = apply_gates([gates.H(0), gates.RY(0, theta=theta)], nqubits=1)
    phase = np.exp(1j * theta / 2.0)
    gate = np.array([[phase.real, -phase.imag],
                     [phase.imag, phase.real]])
    target_state = gate.dot(np.ones(2)) / np.sqrt(2)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
示例#28
0
def test_distributed_qft_execution(nqubits, accelerators):
    original_backend = qibo.get_backend()
    qibo.set_backend("custom")
    dist_c = models.QFT(nqubits, accelerators=accelerators)
    c = models.QFT(nqubits)

    initial_state = utils.random_numpy_state(nqubits)
    final_state = dist_c(initial_state).numpy()
    target_state = c(initial_state).numpy()
    np.testing.assert_allclose(target_state, final_state)
    qibo.set_backend(original_backend)
示例#29
0
def test_construct_unitary_rotations(backend, gate, target_matrix):
    """Check that `construct_unitary` method constructs the proper matrix."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    if gate == "CU1":
        gate = getattr(gates, gate)(0, 1, theta)
    else:
        gate = getattr(gates, gate)(0, theta)
    np.testing.assert_allclose(gate.unitary, target_matrix(theta))
    qibo.set_backend(original_backend)
示例#30
0
def test_dagger(backend, gate, args):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    gate = getattr(gates, gate)(*args)
    nqubits = len(gate.qubits)
    c = Circuit(nqubits)
    c.add((gate, gate.dagger()))
    initial_state = random_state(nqubits)
    final_state = c(np.copy(initial_state))
    np.testing.assert_allclose(final_state, initial_state)
    qibo.set_backend(original_backend)