def test_rx(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) theta = 0.1234 final_state = apply_gates( [gates.H(0), gates.RX(0, theta=theta)], nqubits=1) phase = np.exp(1j * theta / 2.0) gate = np.array([[phase.real, -1j * phase.imag], [-1j * 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)
def test_switcher_warnings(): """Check set precision and backend warnings.""" import qibo from qibo import gates g = gates.H(0) qibo.set_precision("double") with pytest.warns(RuntimeWarning): qibo.set_precision("single") qibo.set_precision("double") with pytest.warns(RuntimeWarning): qibo.set_backend("matmuleinsum") qibo.set_backend("custom")
def QFT(nqubits: int, with_swaps: bool = True, accelerators: Optional[Dict[str, int]] = None, memory_device: str = "/CPU:0") -> Circuit: """Creates a circuit that implements the Quantum Fourier Transform. Args: nqubits (int): Number of qubits in the circuit. with_swaps (bool): Use SWAP gates at the end of the circuit so that the qubit order in the final state is the same as the initial state. accelerators (dict): Accelerator device dictionary in order to use a distributed circuit If ``None`` a simple (non-distributed) circuit will be used. memory_device (str): Device to use for memory in case a distributed circuit is used. Ignored for non-distributed circuits. Returns: A qibo.models.Circuit that implements the Quantum Fourier Transform. Example: :: import numpy as np from qibo.models import QFT nqubits = 6 c = QFT(nqubits) # Random normalized initial state vector init_state = np.random.random(2 ** nqubits) + 1j * np.random.random(2 ** nqubits) init_state = init_state / np.sqrt((np.abs(init_state)**2).sum()) # Execute the circuit final_state = c(init_state) """ if accelerators is not None: if not with_swaps: raise_error(NotImplementedError, "Distributed QFT is only implemented " "with SWAPs.") return _DistributedQFT(nqubits, accelerators, memory_device) from qibo import gates circuit = Circuit(nqubits) for i1 in range(nqubits): circuit.add(gates.H(i1)) for i2 in range(i1 + 1, nqubits): theta = math.pi / 2**(i2 - i1) circuit.add(gates.CU1(i2, i1, theta)) if with_swaps: for i in range(nqubits // 2): circuit.add(gates.SWAP(i, nqubits - i - 1)) return circuit
def test_circuit_decompose_execution(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) c = Circuit(6) c.add(gates.RX(0, 0.1234)) c.add(gates.RY(1, 0.4321)) c.add((gates.H(i) for i in range(2, 6))) c.add(gates.CNOT(0, 1)) c.add(gates.X(3).controlled_by(0, 1, 2, 4)) decomp_c = c.decompose(5) np.testing.assert_allclose(c(), decomp_c(), atol=1e-6) qibo.set_backend(original_backend)
def test_circuit_with_noise_with_measurements(): """Check ``circuit.with_noise() when using measurement noise.""" original_backend = qibo.get_backend() qibo.set_backend("matmuleinsum") c = models.Circuit(2) c.add([gates.H(0), gates.H(1)]) c.add(gates.M(0)) noisy_c = c.with_noise(3 * (0.1, ), measurement_noise=(0.3, 0.0, 0.0)) target_c = models.Circuit(2) target_c.add(gates.H(0)) target_c.add(gates.NoiseChannel(0, 0.1, 0.1, 0.1)) target_c.add(gates.NoiseChannel(1, 0.1, 0.1, 0.1)) target_c.add(gates.H(1)) target_c.add(gates.NoiseChannel(0, 0.3, 0.0, 0.0)) target_c.add(gates.NoiseChannel(1, 0.1, 0.1, 0.1)) final_state = noisy_c().numpy() target_state = target_c().numpy() np.testing.assert_allclose(target_state, final_state) qibo.set_backend(original_backend)
def test_set_backend_errors(): original_backend = backends.get_backend() with pytest.raises(ValueError): backends.set_backend("test") with pytest.raises(ValueError): backends.set_backend("numpy_custom") with pytest.raises(ValueError): backends.set_backend("numpy_badgates") h = gates.H(0) with pytest.warns(RuntimeWarning): backends.set_backend("numpy_defaulteinsum") backends.set_backend(original_backend)
def test_construct_unitary(backend): original_backend = qibo.get_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.U1(0, theta).unitary, target_matrix) target_matrix = np.diag([1, 1, 1, np.exp(1j * theta)]) np.testing.assert_allclose(gates.CU1(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) qibo.set_backend(original_backend)
def test_entropy_in_compiled_circuit(backend): """Check that entropy calculation works when circuit is compiled.""" from qibo import get_backend entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2) c.add(gates.CallbackGate(entropy)) c.add(gates.H(0)) c.add(gates.CallbackGate(entropy)) c.add(gates.CNOT(0, 1)) c.add(gates.CallbackGate(entropy)) c.compile() final_state = c() K.assert_allclose(entropy[:], [0, 0, 1.0], atol=_atol)
def test_probabilistic_measurement(accelerators): import tensorflow as tf tf.random.set_seed(1234) c = models.Circuit(2, accelerators) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.M(0, 1)) result = c(nshots=1000) # update reference values based on device if tf.config.list_physical_devices("GPU"): # pragma: no cover # case not tested in GitHub workflows because it requires GPU decimal_freqs = {0: 273, 1: 233, 2: 242, 3: 252} binary_freqs = {"00": 273, "01": 233, "10": 242, "11": 252} else: decimal_freqs = {0: 271, 1: 239, 2: 242, 3: 248} binary_freqs = {"00": 271, "01": 239, "10": 242, "11": 248} assert sum(binary_freqs.values()) == 1000 assert_results(result, decimal_frequencies=decimal_freqs, binary_frequencies=binary_freqs)
def test_entropy_in_circuit(accelerators): """Check that entropy calculation works in circuit.""" entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add(gates.CallbackGate(entropy)) c.add(gates.H(0)) c.add(gates.CallbackGate(entropy)) c.add(gates.CNOT(0, 1)) c.add(gates.CallbackGate(entropy)) state = c() target = [0, 0, 1.0] np.testing.assert_allclose(entropy[:].numpy(), target, atol=_atol)
def test_hgate_density_matrix(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) initial_rho = random_density_matrix(2) gate = gates.H(1) gate.density_matrix = True final_rho = gate(np.copy(initial_rho)) matrix = np.array([[1, 1], [1, -1]]) / np.sqrt(2) matrix = np.kron(np.eye(2), matrix) target_rho = matrix.dot(initial_rho).dot(matrix) np.testing.assert_allclose(final_rho, target_rho) qibo.set_backend(original_backend)
def test_multiple_noise(backend): """Test `gates.NoiseChnanel` with multiple noise probabilities.""" from qibo import matrices original_backend = qibo.get_backend() qibo.set_backend(backend) c = models.Circuit(2, density_matrix=True) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.PauliNoiseChannel(0, px=0.5, pz=0.3)) c.add(gates.PauliNoiseChannel(1, py=0.1, pz=0.3)) final_rho = c().numpy() psi = np.ones(4) / 2 rho = np.outer(psi, psi.conj()) m1 = np.kron(matrices.X, matrices.I) m2 = np.kron(matrices.Z, matrices.I) rho = 0.2 * rho + 0.5 * m1.dot(rho.dot(m1)) + 0.3 * m2.dot(rho.dot(m2)) m1 = np.kron(matrices.I, matrices.Y) m2 = np.kron(matrices.I, matrices.Z) rho = 0.6 * rho + 0.1 * m1.dot(rho.dot(m1)) + 0.3 * m2.dot(rho.dot(m2)) np.testing.assert_allclose(final_rho, rho) qibo.set_backend(original_backend)
def test_fsim(backend): """Check fSim gate is working properly on |++>.""" original_backend = qibo.get_backend() qibo.set_backend(backend) theta = 0.1234 phi = 0.4321 c = Circuit(2) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.fSim(0, 1, theta, phi)) final_state = c.execute().numpy() target_state = np.ones_like(final_state) / 2.0 rotation = np.array([[np.cos(theta), -1j * np.sin(theta)], [-1j * np.sin(theta), np.cos(theta)]]) matrix = np.eye(4, dtype=target_state.dtype) matrix[1:3, 1:3] = rotation matrix[3, 3] = np.exp(-1j * phi) target_state = matrix.dot(target_state) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_entropy_in_distributed_circuit(backend, accelerators, gateconf, target_entropy): """Check that various entropy configurations work in distributed circuit.""" original_backend = qibo.get_backend() qibo.set_backend(backend) target_c = Circuit(4) target_c.add([gates.H(0), gates.CNOT(0, 1)]) target_state = target_c() entropy = callbacks.EntanglementEntropy([0]) c = Circuit(4, accelerators) for gate in gateconf: if gate == "H": c.add(gates.H(0)) elif gate == "CNOT": c.add(gates.CNOT(0, 1)) elif gate == "entropy": c.add(gates.CallbackGate(entropy)) final_state = c() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:], target_entropy, atol=_atol) qibo.set_backend(original_backend)
def test_density_matrix_circuit(backend): from qibo.tests.utils import random_density_matrix theta = 0.1234 initial_rho = random_density_matrix(3) c = Circuit(3, density_matrix=True) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.CNOT(0, 1)) c.add(gates.H(2)) final_rho = c(np.copy(initial_rho)) h = np.array([[1, 1], [1, -1]]) / np.sqrt(2) cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) m1 = np.kron(np.kron(h, h), np.eye(2)) m2 = np.kron(cnot, np.eye(2)) m3 = np.kron(np.eye(4), h) target_rho = m1.dot(initial_rho).dot(m1.T.conj()) target_rho = m2.dot(target_rho).dot(m2.T.conj()) target_rho = m3.dot(target_rho).dot(m3.T.conj()) K.assert_allclose(final_rho, target_rho)
def test_circuit_with_noise_noise_map(backend): """Check ``circuit.with_noise() when giving noise map.""" original_backend = qibo.get_backend() qibo.set_backend(backend) noise_map = {0: (0.1, 0.2, 0.1), 1: (0.2, 0.3, 0.0), 2: (0.0, 0.0, 0.0)} c = models.Circuit(3, density_matrix=True) c.add([gates.H(0), gates.H(1), gates.X(2)]) c.add(gates.M(2)) noisy_c = c.with_noise(noise_map) target_c = models.Circuit(3, density_matrix=True) target_c.add(gates.H(0)) target_c.add(gates.PauliNoiseChannel(0, 0.1, 0.2, 0.1)) target_c.add(gates.H(1)) target_c.add(gates.PauliNoiseChannel(1, 0.2, 0.3, 0.0)) target_c.add(gates.X(2)) final_state = noisy_c().numpy() target_state = target_c().numpy() np.testing.assert_allclose(target_state, final_state) qibo.set_backend(original_backend)
def test_set_backend(backend_name): """Check ``set_backend`` for switching gate backends.""" original_backend = backends.get_backend() backends.set_backend(backend_name) assert K.name == backend_name assert str(K) == backend_name assert repr(K) == backend_name assert K.executing_eagerly() h = gates.H(0) if backend_name == "qibotf" or backend_name == "qibojit": assert h.gate_op else: assert h.gate_op is None backends.set_backend(original_backend)
def test_circuit_decompose(): """Check ``circuit.decompose`` agrees with multi-control ``X`` decomposition.""" c = Circuit(6) c.add(gates.RX(0, 0.1234)) c.add(gates.RY(1, 0.4321)) c.add((gates.H(i) for i in range(2, 6))) c.add(gates.CNOT(0, 1)) c.add(gates.X(3).controlled_by(0, 1, 2, 4)) decomp_c = c.decompose(5) init_state = utils.random_numpy_state(c.nqubits) target_state = c(np.copy(init_state)).numpy() final_state = decomp_c(np.copy(init_state)).numpy() np.testing.assert_allclose(final_state, target_state, atol=_ATOL) target_c = Circuit(c.nqubits) target_c.add(gates.RX(0, 0.1234)) target_c.add(gates.RY(1, 0.4321)) target_c.add((gates.H(i) for i in range(2, 6))) target_c.add(gates.CNOT(0, 1)) target_c.add(gates.X(3).controlled_by(0, 1, 2, 4).decompose(5)) assert_circuit_same_gates(decomp_c, target_c)
def test_controlled_unitary(backend, accelerators): original_backend = qibo.get_backend() qibo.set_backend(backend) matrix = np.random.random((2, 2)) c = Circuit(2) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.Unitary(matrix, 1).controlled_by(0)) final_state = c.execute() target_state = np.ones_like(final_state) / 2.0 target_state[2:] = matrix.dot(target_state[2:]) np.testing.assert_allclose(final_state, target_state) matrix = np.random.random((4, 4)) c = Circuit(4, accelerators) c.add((gates.H(i) for i in range(4))) c.add(gates.Unitary(matrix, 1, 3).controlled_by(0, 2)) final_state = c.execute() target_state = np.ones_like(final_state) / 4.0 ids = [10, 11, 14, 15] target_state[ids] = matrix.dot(target_state[ids]) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_callbacks_fusion(backend): """Check entropy calculation in fused circuit.""" from qibo import callbacks entropy = callbacks.EntanglementEntropy([0]) c = Circuit(5) c.add(gates.H(0)) c.add(gates.X(1)) c.add(gates.CallbackGate(entropy)) c.add(gates.CNOT(0, 1)) c.add(gates.CallbackGate(entropy)) fused_c = c.fuse() K.assert_allclose(fused_c(), c()) target_entropy = [0.0, 1.0, 0.0, 1.0] K.assert_allclose(entropy[:], target_entropy, atol=1e-7)
def test_fuse_circuit_with_controlled_by_gates(): """Check gate fusion in circuit that contains ``controlled_by`` gates.""" c = Circuit(4) c.add((gates.H(i) for i in range(4))) c.add(gates.RX(1, theta=0.1234).controlled_by(0)) c.add(gates.RX(3, theta=0.4321).controlled_by(2)) c.add((gates.RY(i, theta=0.5678) for i in range(4))) c.add(gates.RX(1, theta=0.1234).controlled_by(0)) c.add(gates.RX(3, theta=0.4321).controlled_by(2)) fused_c = c.fuse() target_state = c() final_state = fused_c() np.testing.assert_allclose(final_state, target_state)
def test_collapse_after_measurement(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) qubits = [0, 2, 3] c1 = Circuit(5) c1.add((gates.H(i) for i in range(5))) c1.add(gates.M(*qubits)) result = c1(nshots=1) c2 = Circuit(5) bitstring = result.samples(binary=True)[0] c2.add(gates.Collapse(*qubits, result=bitstring)) c2.add((gates.H(i) for i in range(5))) final_state = c2(initial_state=c1.final_state) ct = Circuit(5) for i, r in zip(qubits, bitstring): if r: ct.add(gates.X(i)) ct.add((gates.H(i) for i in qubits)) target_state = ct() np.testing.assert_allclose(final_state, target_state, atol=1e-15) qibo.set_backend(original_backend)
def test_set_parameters_with_list(backend, trainable): """Check updating parameters of circuit with list.""" original_backend = qibo.get_backend() qibo.set_backend(backend) params = [0.123, 0.456, (0.789, 0.321)] c = Circuit(3) if trainable: c.add(gates.RX(0, theta=0, trainable=trainable)) else: c.add(gates.RX(0, theta=params[0], trainable=trainable)) c.add(gates.RY(1, theta=0)) c.add(gates.CZ(1, 2)) c.add(gates.fSim(0, 2, theta=0, phi=0)) c.add(gates.H(2)) # execute once final_state = c() target_c = Circuit(3) target_c.add(gates.RX(0, theta=params[0])) target_c.add(gates.RY(1, theta=params[1])) target_c.add(gates.CZ(1, 2)) target_c.add(gates.fSim(0, 2, theta=params[2][0], phi=params[2][1])) target_c.add(gates.H(2)) # Attempt using a flat np.ndarray/list for new_params in (np.random.random(4), list(np.random.random(4))): if trainable: c.set_parameters(new_params) else: new_params[0] = params[0] c.set_parameters(new_params[1:]) target_params = [ new_params[0], new_params[1], (new_params[2], new_params[3]) ] target_c.set_parameters(target_params) np.testing.assert_allclose(c(), target_c()) qibo.set_backend(original_backend)
def test_unitary_controlled_by(backend, accelerators): """Check that `controlled_by` works as expected with `Unitary`.""" original_backend = qibo.get_backend() qibo.set_backend(backend) matrix = np.random.random([2, 2]) c = Circuit(2, accelerators) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.Unitary(matrix, 1).controlled_by(0)) final_state = c.execute().numpy() target_state = np.ones_like(final_state) / 2.0 target_state[2:] = matrix.dot(target_state[2:]) np.testing.assert_allclose(final_state, target_state) matrix = np.random.random([4, 4]) c = Circuit(4, accelerators) c.add((gates.H(i) for i in range(4))) c.add(gates.Unitary(matrix, 1, 3).controlled_by(0, 2)) final_state = c.execute().numpy() target_state = np.ones_like(final_state) / 4.0 ids = [10, 11, 14, 15] target_state[ids] = matrix.dot(target_state[ids]) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_entropy_in_distributed_circuit(): """Check that various entropy configurations work in distributed circuit.""" target_c = Circuit(2) target_c.add([gates.H(0), gates.CNOT(0, 1)]) target_state = target_c().numpy() accelerators = {"/GPU:0": 1, "/GPU:1": 1} entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add([gates.H(0), gates.CNOT(0, 1), gates.CallbackGate(entropy)]) final_state = c().numpy() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:].numpy(), [1.0], atol=_atol) entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add([gates.H(0), gates.CallbackGate(entropy), gates.CNOT(0, 1)]) final_state = c().numpy() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:].numpy(), [0.0], atol=_atol) entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add([gates.CallbackGate(entropy), gates.H(0), gates.CNOT(0, 1)]) final_state = c().numpy() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:].numpy(), [0.0], atol=_atol) entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add([gates.CallbackGate(entropy), gates.H(0), gates.CNOT(0, 1), gates.CallbackGate(entropy)]) final_state = c().numpy() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:].numpy(), [0, 1.0], atol=_atol) entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add([gates.H(0), gates.CallbackGate(entropy), gates.CNOT(0, 1), gates.CallbackGate(entropy)]) final_state = c().numpy() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:].numpy(), [0, 1.0], atol=_atol) entropy = callbacks.EntanglementEntropy([0]) c = Circuit(2, accelerators) c.add([gates.CallbackGate(entropy), gates.H(0), gates.CallbackGate(entropy), gates.CNOT(0, 1)]) final_state = c().numpy() np.testing.assert_allclose(final_state, target_state) np.testing.assert_allclose(entropy[:].numpy(), [0, 0], atol=_atol)
def test_circuit_unitary_bigger(backend): from qibo import matrices c = Circuit(4) c.add(gates.H(i) for i in range(4)) c.add(gates.CNOT(0, 1)) c.add(gates.CZ(1, 2)) c.add(gates.CNOT(0, 3)) h = np.kron(matrices.H, matrices.H) h = np.kron(h, h) m1 = np.kron(matrices.CNOT, np.eye(4)) m2 = np.kron(np.kron(np.eye(2), matrices.CZ), np.eye(2)) m3 = np.kron(matrices.CNOT, np.eye(4)).reshape(8 * (2, )) m3 = np.transpose(m3, [0, 2, 3, 1, 4, 6, 7, 5]).reshape((16, 16)) target_matrix = m3 @ m2 @ m1 @ h K.assert_allclose(c.unitary(), target_matrix)
def test_transform_queue_more_gates(): original_backend = qibo.get_backend() qibo.set_backend("custom") devices = {"/GPU:0": 2, "/GPU:1": 2} c = models.DistributedCircuit(4, devices) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.CNOT(2, 3)) c.add(gates.CZ(0, 1)) c.add(gates.CNOT(3, 0)) c.add(gates.CNOT(1, 2)) c.queues.qubits = distutils.DistributedQubits([2, 3], c.nqubits) tqueue = c.queues.transform(c.queue) assert len(tqueue) == 10 assert isinstance(tqueue[0], gates.H) assert tqueue[0].target_qubits == (0, ) assert isinstance(tqueue[1], gates.H) assert tqueue[1].target_qubits == (1, ) assert isinstance(tqueue[2], gates.CZ) assert tqueue[2].target_qubits == (1, ) assert isinstance(tqueue[3], gates.SWAP) assert set(tqueue[3].target_qubits) == {1, 3} assert isinstance(tqueue[4], gates.CNOT) assert tqueue[4].target_qubits == (1, ) assert isinstance(tqueue[5], gates.CNOT) assert tqueue[5].target_qubits == (0, ) assert isinstance(tqueue[6], gates.SWAP) assert set(tqueue[6].target_qubits) == {0, 2} assert isinstance(tqueue[7], gates.CNOT) assert tqueue[7].target_qubits == (0, ) assert isinstance(tqueue[8], gates.SWAP) assert set(tqueue[8].target_qubits) == {0, 2} assert isinstance(tqueue[9], gates.SWAP) assert set(tqueue[9].target_qubits) == {1, 3} qibo.set_backend(original_backend)
def test_circuit_on_qubits_controlled_by_execution(backend, accelerators): original_backend = qibo.get_backend() qibo.set_backend(backend) smallc = Circuit(3) smallc.add(gates.RX(0, theta=0.1).controlled_by(1, 2)) smallc.add(gates.RY(1, theta=0.2).controlled_by(0)) smallc.add(gates.RX(2, theta=0.3).controlled_by(1, 0)) smallc.add(gates.RZ(1, theta=0.4).controlled_by(0, 2)) largec = Circuit(6, accelerators=accelerators) largec.add(gates.H(i) for i in range(6)) largec.add(smallc.on_qubits(1, 4, 3)) targetc = Circuit(6) targetc.add(gates.H(i) for i in range(6)) targetc.add(gates.RX(1, theta=0.1).controlled_by(3, 4)) targetc.add(gates.RY(4, theta=0.2).controlled_by(1)) targetc.add(gates.RX(3, theta=0.3).controlled_by(1, 4)) targetc.add(gates.RZ(4, theta=0.4).controlled_by(1, 3)) assert largec.depth == targetc.depth np.testing.assert_allclose(largec(), targetc()) qibo.set_backend(original_backend)
def test_hgate_application_twoqubit(backend): """Check applying one qubit gate to two qubit density matrix.""" original_backend = qibo.get_backend() qibo.set_backend(backend) initial_rho = random_density_matrix(2) gate = gates.H(1) final_rho = gate(initial_rho.reshape(4 * (2,)), is_density_matrix=True ).numpy().reshape((4, 4)) matrix = np.array([[1, 1], [1, -1]]) / np.sqrt(2) matrix = np.kron(np.eye(2), matrix) target_rho = matrix.dot(initial_rho).dot(matrix) np.testing.assert_allclose(final_rho, target_rho) qibo.set_backend(original_backend)
def i_qft(q): """(Inverse) Quantum Fourier Transform on a quantum register. Args: q (list): quantum register where the QFT is applied. Returns: generator with the required quantum gates applied on the quantum circuit. """ for i in range(len(q) // 2): yield gates.SWAP(i, len(q) - i - 1) for i1 in reversed(range(len(q))): for i2 in reversed(range(i1 + 1, len(q))): theta = np.pi / 2 ** (i2 - i1) yield gates.CU1(q[i2], q[i1], -theta) yield gates.H(q[i1])