def test_generalized_fsim_parameter_setter(backend): phi = np.random.random() matrix = np.random.random((2, 2)) gate = gates.GeneralizedfSim(0, 1, matrix, phi) K.assert_allclose(gate.parameters[0], matrix) assert gate.parameters[1] == phi matrix = np.random.random((4, 4)) with pytest.raises(ValueError): gate = gates.GeneralizedfSim(0, 1, matrix, phi)
def test_generalized_fsim_parameter_setter(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) phi = np.random.random() matrix = np.random.random((2, 2)) gate = gates.GeneralizedfSim(0, 1, matrix, phi) np.testing.assert_allclose(gate.parameters[0], matrix) assert gate.parameters[1] == phi matrix = np.random.random((4, 4)) with pytest.raises(ValueError): gate = gates.GeneralizedfSim(0, 1, matrix, phi) qibo.set_backend(original_backend)
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)
def test_generalizedfsim_dagger(backend): from scipy.linalg import expm phi = 0.2 matrix = np.random.random((2, 2)) matrix = expm(1j * (matrix + matrix.T)) gate = gates.GeneralizedfSim(0, 1, matrix, phi) c = Circuit(2) c.add((gate, gate.dagger())) initial_state = random_state(2) final_state = c(np.copy(initial_state)) K.assert_allclose(final_state, initial_state)
def test_generalized_fsim(backend): phi = np.random.random() rotation = np.random.random((2, 2)) + 1j * np.random.random((2, 2)) gatelist = [gates.H(0), gates.H(1), gates.H(2)] gatelist.append(gates.GeneralizedfSim(1, 2, rotation, phi)) final_state = apply_gates(gatelist, nqubits=3) target_state = np.ones_like(K.to_numpy(final_state)) / np.sqrt(8) matrix = np.eye(4, dtype=target_state.dtype) matrix[1:3, 1:3] = rotation matrix[3, 3] = np.exp(-1j * phi) target_state[:4] = matrix.dot(target_state[:4]) target_state[4:] = matrix.dot(target_state[4:]) K.assert_allclose(final_state, target_state)
def test_generalized_fsim(backend, accelerators): """Check GeneralizedfSim gate is working properly on |++>.""" original_backend = qibo.get_backend() qibo.set_backend(backend) phi = np.random.random() rotation = utils.random_numpy_complex((2, 2)) c = Circuit(3, accelerators) c.add((gates.H(i) for i in range(3))) c.add(gates.GeneralizedfSim(1, 2, rotation, phi)) final_state = c.execute().numpy() target_state = np.ones_like(final_state) / np.sqrt(8) matrix = np.eye(4, dtype=target_state.dtype) matrix[1:3, 1:3] = rotation matrix[3, 3] = np.exp(-1j * phi) target_state[:4] = matrix.dot(target_state[:4]) target_state[4:] = matrix.dot(target_state[4:]) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_generalizedfsim_dagger(backend, tfmatrix): from scipy.linalg import expm original_backend = qibo.get_backend() qibo.set_backend(backend) phi = 0.2 matrix = np.random.random((2, 2)) matrix = expm(1j * (matrix + matrix.T)) if tfmatrix: import tensorflow as tf from qibo.config import DTYPES matrix = tf.cast(matrix, dtype=DTYPES.get('DTYPECPX')) gate = gates.GeneralizedfSim(0, 1, matrix, phi) c = Circuit(2) c.add((gate, gate.dagger())) initial_state = utils.random_numpy_state(2) final_state = c(np.copy(initial_state)).numpy() np.testing.assert_allclose(final_state, initial_state) qibo.set_backend(original_backend)