def test_rx_parameter_setter(backend): """Check that the parameter setter of RX gate is working properly.""" def exact_state(theta): phase = np.exp(1j * theta / 2.0) gate = np.array([[phase.real, -1j * phase.imag], [-1j * phase.imag, phase.real]]) return gate.dot(np.ones(2)) / np.sqrt(2) original_backend = qibo.get_backend() qibo.set_backend(backend) theta = 0.1234 c = Circuit(1) c.add(gates.H(0)) c.add(gates.RX(0, theta=theta)) final_state = c().numpy() target_state = exact_state(theta) np.testing.assert_allclose(final_state, target_state) theta = 0.4321 c.queue[-1].parameter = theta final_state = c().numpy() target_state = exact_state(theta) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_post_measurement_bitflips_on_circuit_result(): """Check bitflip errors on ``CircuitResult`` objects.""" import tensorflow as tf from qibo.config import DTYPES thetas = np.random.random(4) c = models.Circuit(4) c.add((gates.RX(i, theta=t) for i, t in enumerate(thetas))) c.add(gates.M(0, 1, register_name="a")) c.add(gates.M(3, register_name="b")) result = c(nshots=30) tf.random.set_seed(123) noisy_result = result.apply_bitflips({0: 0.2, 1: 0.4, 3: 0.3}) noisy_samples = noisy_result.samples(binary=True) register_samples = noisy_result.samples(binary=True, registers=True) samples = result.samples().numpy() tf.random.set_seed(123) sprobs = tf.random.uniform(samples.shape, dtype=DTYPES.get('DTYPE')) flipper = sprobs.numpy() < np.array([0.2, 0.4, 0.3]) target_samples = (samples + flipper) % 2 np.testing.assert_allclose(noisy_samples, target_samples) # Check register samples np.testing.assert_allclose(register_samples["a"], target_samples[:, :2]) np.testing.assert_allclose(register_samples["b"], target_samples[:, 2:])
def test_rx_parameter_setter(backend): """Check the parameter setter of RX gate.""" def exact_state(theta): phase = np.exp(1j * theta / 2.0) gate = np.array([[phase.real, -1j * phase.imag], [-1j * phase.imag, phase.real]]) return gate.dot(np.ones(2)) / np.sqrt(2) original_backend = qibo.get_backend() qibo.set_backend(backend) theta = 0.1234 gate = gates.RX(0, theta=theta) initial_state = K.cast(np.ones(2) / np.sqrt(2)) final_state = gate(initial_state) target_state = exact_state(theta) np.testing.assert_allclose(final_state, target_state) theta = 0.4321 gate.parameters = theta initial_state = K.cast(np.ones(2) / np.sqrt(2)) final_state = gate(initial_state) target_state = exact_state(theta) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def rotations(): for q in range(self.nqubits): yield gates.RX(q, theta=0) yield gates.RZ(q, theta=0) yield gates.RX(q, theta=0)
def last_rotation(circuit, nqubits): for k in range(nqubits): circuit.add(gates.RZ(k, np.random.rand())) circuit.add(gates.RX(k, np.random.rand()))