def test_circuit_on_qubits_with_unitary_execution(backend, accelerators, controlled): unitaries = np.random.random((2, 2, 2)) smallc = Circuit(2) if controlled: smallc.add(gates.Unitary(unitaries[0], 0).controlled_by(1)) smallc.add(gates.Unitary(unitaries[1], 1).controlled_by(0)) else: smallc.add(gates.Unitary(unitaries[0], 0)) smallc.add(gates.Unitary(unitaries[1], 1)) smallc.add(gates.CNOT(0, 1)) largec = Circuit(4, accelerators=accelerators) largec.add(gates.RY(0, theta=0.1)) largec.add(gates.RY(1, theta=0.2)) largec.add(gates.RY(2, theta=0.3)) largec.add(gates.RY(3, theta=0.2)) largec.add(smallc.on_qubits(3, 0)) targetc = Circuit(4) targetc.add(gates.RY(0, theta=0.1)) targetc.add(gates.RY(1, theta=0.2)) targetc.add(gates.RY(2, theta=0.3)) targetc.add(gates.RY(3, theta=0.2)) if controlled: targetc.add(gates.Unitary(unitaries[0], 3).controlled_by(0)) targetc.add(gates.Unitary(unitaries[1], 0).controlled_by(3)) else: targetc.add(gates.Unitary(unitaries[0], 3)) targetc.add(gates.Unitary(unitaries[1], 0)) targetc.add(gates.CNOT(3, 0)) assert largec.depth == targetc.depth K.assert_allclose(largec(), targetc())
def test_reset_error(backend, density_matrix): reset = ResetError(0.8, 0.2) noise = NoiseModel() noise.add(reset, gates.X, 1) noise.add(reset, gates.CNOT) noise.add(reset, gates.Z, (0,1)) circuit = Circuit(3, density_matrix=density_matrix) circuit.add(gates.CNOT(0,1)) circuit.add(gates.Z(1)) target_circuit = Circuit(3, density_matrix=density_matrix) target_circuit.add(gates.CNOT(0,1)) target_circuit.add(gates.ResetChannel(0, 0.8, 0.2)) target_circuit.add(gates.ResetChannel(1, 0.8, 0.2)) target_circuit.add(gates.Z(1)) target_circuit.add(gates.ResetChannel(1, 0.8, 0.2)) initial_psi = random_density_matrix(3) if density_matrix else random_state(3) np.random.seed(123) K.set_seed(123) final_state = noise.apply(circuit)(initial_state=np.copy(initial_psi)) np.random.seed(123) K.set_seed(123) target_final_state = target_circuit(initial_state=np.copy(initial_psi)) K.assert_allclose(final_state, target_final_state)
def test_entropy_multiple_executions(accelerators): """Check entropy calculation when the callback is used in multiple executions.""" entropy = callbacks.EntanglementEntropy([0]) target_c = Circuit(2) target_c.add([gates.RY(0, 0.1234), gates.CNOT(0, 1)]) target_state = target_c().numpy() c = Circuit(2, accelerators) c.add(gates.RY(0, 0.1234)) c.add(gates.CallbackGate(entropy)) c.add(gates.CNOT(0, 1)) c.add(gates.CallbackGate(entropy)) state = c() np.testing.assert_allclose(state.numpy(), target_state) target_c = Circuit(2) target_c.add([gates.RY(0, 0.4321), gates.CNOT(0, 1)]) target_state = target_c().numpy() c = Circuit(2, accelerators) c.add(gates.RY(0, 0.4321)) c.add(gates.CallbackGate(entropy)) c.add(gates.CNOT(0, 1)) c.add(gates.CallbackGate(entropy)) state = c() np.testing.assert_allclose(state.numpy(), target_state) def target_entropy(t): cos = np.cos(t / 2.0)**2 sin = np.sin(t / 2.0)**2 return -cos * np.log2(cos) - sin * np.log2(sin) target = [0, target_entropy(0.1234), 0, target_entropy(0.4321)] np.testing.assert_allclose(entropy[:].numpy(), target, atol=_atol)
def test_transform_queue_more_gates(backend): devices = {"/GPU:0": 2, "/GPU:1": 2} c = 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 = 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}
def test_thermal_error(backend, density_matrix): thermal = ThermalRelaxationError(2, 1, 0.3) noise = NoiseModel() noise.add(thermal, gates.X, 1) noise.add(thermal, gates.CNOT) noise.add(thermal, gates.Z, (0,1)) circuit = Circuit(3, density_matrix=density_matrix) circuit.add(gates.CNOT(0,1)) circuit.add(gates.Z(1)) circuit.add(gates.X(1)) circuit.add(gates.X(2)) circuit.add(gates.Z(2)) target_circuit = Circuit(3, density_matrix=density_matrix) target_circuit.add(gates.CNOT(0,1)) target_circuit.add(gates.ThermalRelaxationChannel(0, 2, 1, 0.3)) target_circuit.add(gates.ThermalRelaxationChannel(1, 2, 1, 0.3)) target_circuit.add(gates.Z(1)) target_circuit.add(gates.ThermalRelaxationChannel(1, 2, 1, 0.3)) target_circuit.add(gates.X(1)) target_circuit.add(gates.ThermalRelaxationChannel(1, 2, 1, 0.3)) target_circuit.add(gates.X(2)) target_circuit.add(gates.Z(2)) initial_psi = random_density_matrix(3) if density_matrix else random_state(3) np.random.seed(123) K.set_seed(123) final_state = noise.apply(circuit)(initial_state=np.copy(initial_psi)) np.random.seed(123) K.set_seed(123) target_final_state = target_circuit(initial_state=np.copy(initial_psi)) K.assert_allclose(final_state, target_final_state)
def test_distributed_circuit_addition(): # Attempt to add circuits with different devices original_backend = qibo.get_backend() qibo.set_backend("custom") devices = {"/GPU:0": 2, "/GPU:1": 2} c1 = models.DistributedCircuit(6, devices) c2 = models.DistributedCircuit(6, {"/GPU:0": 2}) with pytest.raises(ValueError): c = c1 + c2 c2 = models.DistributedCircuit(6, devices) c1.add([gates.H(i) for i in range(6)]) c2.add([gates.CNOT(i, i + 1) for i in range(5)]) c2.add([gates.Z(i) for i in range(6)]) dist_c = c1 + c2 c = models.Circuit(6) c.add([gates.H(i) for i in range(6)]) c.add([gates.CNOT(i, i + 1) for i in range(5)]) c.add([gates.Z(i) for i in range(6)]) target_state = c().numpy() final_state = dist_c().numpy() assert c.depth == dist_c.depth np.testing.assert_allclose(target_state, final_state) qibo.set_backend(original_backend)
def r_g(a, b, x, y, anc, rot): """Reversed basic component for the BLAKE construction. Args: a (list): quantum register part of the permutation matrix v. b (list): quantum register part of the permutation matrix v. x (list): quantum register part of the message digest d. y (list): quantum register part of the message digest d. anc (int): ancilliary qubit for the modular addition circuit. rot (list): characterization of the rotations performed. Returns: generator of the quantum gates requires to apply the circuit. """ n = int(len(a)) for i in range(rot[0]): b = b[1:] + [b[0]] for i in reversed(range(n)): yield gates.CNOT(a[i], b[i]) yield r_adder_mod2n(y, a, anc) yield r_adder_mod2n(b, a, anc) for i in range(rot[1]): b = b[1:] + [b[0]] for i in reversed(range(n)): yield gates.CNOT(a[i], b[i]) yield r_adder_mod2n(x, a, anc) yield r_adder_mod2n(b, a, anc)
def test_circuit_on_qubits_double_execution(backend, accelerators, distribute_small): original_backend = qibo.get_backend() qibo.set_backend(backend) if distribute_small: smallc = Circuit(3, accelerators=accelerators) else: smallc = Circuit(3) smallc.add((gates.RX(i, theta=i + 0.1) for i in range(3))) smallc.add((gates.CNOT(0, 1), gates.CZ(1, 2))) # execute the small circuit before adding it to the large one _ = smallc() largec = Circuit(6, accelerators=accelerators) largec.add((gates.RY(i, theta=i + 0.2) for i in range(0, 6, 2))) if distribute_small and accelerators is not None: with pytest.raises(RuntimeError): largec.add(smallc.on_qubits(1, 3, 5)) else: largec.add(smallc.on_qubits(1, 3, 5)) targetc = Circuit(6) targetc.add((gates.RY(i, theta=i + 0.2) for i in range(0, 6, 2))) targetc.add((gates.RX(i, theta=i // 2 + 0.1) for i in range(1, 6, 2))) targetc.add((gates.CNOT(1, 3), gates.CZ(3, 5))) assert largec.depth == targetc.depth np.testing.assert_allclose(largec(), targetc()) qibo.set_backend(original_backend)
def r_qr(a, b, c, d, x, rot): """Reverse circuit for the quantum quarter round for the toy Chacha permutation. Args: a (list): quantum register of a site in the permutation matrix. b (list): quantum register of a site in the permutation matrix. c (list): quantum register of a site in the permutation matrix. d (list): quantum register of a site in the permutation matrix. x (int): ancillary qubit needed for the adder circuit. rot (list): characterization of the rotation part of the algorithm. Returns: quantum gate generator that applies the reversed quantum gates for the Chacha quarter round. """ n = int(len(a)) for i in range(rot[3]): b = [b[-1]] + b[:-1] for i in reversed(range(n)): yield gates.CNOT(c[i], b[i]) yield r_adder_mod2n(d, c, x) for i in range(rot[2]): d = [d[-1]] + d[:-1] for i in reversed(range(n)): yield gates.CNOT(a[i], d[i]) yield r_adder_mod2n(b, a, x) for i in range(rot[1]): b = [b[-1]] + b[:-1] for i in reversed(range(n)): yield gates.CNOT(c[i], b[i]) yield r_adder_mod2n(d, c, x) for i in range(rot[0]): d = [d[-1]] + d[:-1] for i in reversed(range(n)): yield gates.CNOT(a[i], d[i]) yield r_adder_mod2n(b, a, x)
def test_distributed_circuit_execution_controlled_gate(backend, accelerators): dist_c = DistributedCircuit(4, accelerators) dist_c.add((gates.H(i) for i in range(dist_c.nglobal, 4))) dist_c.add(gates.CNOT(0, 2)) c = Circuit(4) c.add((gates.H(i) for i in range(dist_c.nglobal, 4))) c.add(gates.CNOT(0, 2)) initial_state = random_state(c.nqubits) final_state = dist_c(np.copy(initial_state)) target_state = c(np.copy(initial_state)) np.testing.assert_allclose(target_state, final_state)
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_circuit_dm(backend): """Check passing density matrix as initial state to a circuit.""" original_backend = qibo.get_backend() qibo.set_backend(backend) theta = 0.1234 initial_rho = utils.random_density_matrix(3) c = models.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()) np.testing.assert_allclose(final_rho, target_rho) qibo.set_backend(original_backend)
def test_multicontrol_xgate(backend): """Check that fallback method for X works for one or two controls.""" original_backend = qibo.get_backend() qibo.set_backend(backend) c1 = Circuit(3) c1.add(gates.X(0)) c1.add(gates.X(2).controlled_by(0)) final_state = c1.execute().numpy() c2 = Circuit(3) c2.add(gates.X(0)) c2.add(gates.CNOT(0, 2)) target_state = c2.execute().numpy() np.testing.assert_allclose(final_state, target_state) c1 = Circuit(3) c1.add(gates.X(0)) c1.add(gates.X(2)) c1.add(gates.X(1).controlled_by(0, 2)) final_state = c1.execute().numpy() c2 = Circuit(3) c2.add(gates.X(0)) c2.add(gates.X(2)) c2.add(gates.TOFFOLI(0, 2, 1)) target_state = c2.execute().numpy() np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_unitary_common_gates(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) target_state = apply_gates([gates.X(0), gates.H(1)], nqubits=2) gatelist = [gates.Unitary(np.array([[0, 1], [1, 0]]), 0), gates.Unitary(np.array([[1, 1], [1, -1]]) / np.sqrt(2), 1)] final_state = apply_gates(gatelist, nqubits=2) np.testing.assert_allclose(final_state, target_state) thetax = 0.1234 thetay = 0.4321 gatelist = [gates.RX(0, theta=thetax), gates.RY(1, theta=thetay), gates.CNOT(0, 1)] target_state = apply_gates(gatelist, nqubits=2) rx = np.array([[np.cos(thetax / 2), -1j * np.sin(thetax / 2)], [-1j * np.sin(thetax / 2), np.cos(thetax / 2)]]) ry = np.array([[np.cos(thetay / 2), -np.sin(thetay / 2)], [np.sin(thetay / 2), np.cos(thetay / 2)]]) cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) gatelist = [gates.Unitary(rx, 0), gates.Unitary(ry, 1), gates.Unitary(cnot, 0, 1)] final_state = apply_gates(gatelist, nqubits=2) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_unitary_common_gates(backend): """Check that `Unitary` gate can create common gates.""" original_backend = qibo.get_backend() qibo.set_backend(backend) c = Circuit(2) c.add(gates.X(0)) c.add(gates.H(1)) target_state = c.execute().numpy() c = Circuit(2) c.add(gates.Unitary(np.array([[0, 1], [1, 0]]), 0)) c.add(gates.Unitary(np.array([[1, 1], [1, -1]]) / np.sqrt(2), 1)) final_state = c.execute().numpy() np.testing.assert_allclose(final_state, target_state) thetax = 0.1234 thetay = 0.4321 c = Circuit(2) c.add(gates.RX(0, theta=thetax)) c.add(gates.RY(1, theta=thetay)) c.add(gates.CNOT(0, 1)) target_state = c.execute().numpy() c = Circuit(2) rx = np.array([[np.cos(thetax / 2), -1j * np.sin(thetax / 2)], [-1j * np.sin(thetax / 2), np.cos(thetax / 2)]]) ry = np.array([[np.cos(thetay / 2), -np.sin(thetay / 2)], [np.sin(thetay / 2), np.cos(thetay / 2)]]) cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) c.add(gates.Unitary(rx, 0)) c.add(gates.Unitary(ry, 1)) c.add(gates.Unitary(cnot, 0, 1)) final_state = c.execute().numpy() np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)
def test_unitary_multiqubit(backend): gatelist = [gates.H(i) for i in range(4)] gatelist.append(gates.CNOT(0, 1)) gatelist.append(gates.CNOT(2, 3)) gatelist.extend(gates.X(i) for i in range(4)) h = np.array([[1, 1], [1, -1]]) / np.sqrt(2) x = np.array([[0, 1], [1, 0]]) cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) matrix = np.kron(np.kron(x, x), np.kron(x, x)) matrix = matrix @ np.kron(cnot, cnot) matrix = matrix @ np.kron(np.kron(h, h), np.kron(h, h)) unitary = gates.Unitary(matrix, 0, 1, 2, 3) final_state = apply_gates([unitary], nqubits=4) target_state = apply_gates(gatelist, nqubits=4) K.assert_allclose(final_state, target_state)
def test_circuit_addition_execution(backend, accelerators): c1 = Circuit(4, accelerators) c1.add(gates.H(0)) c1.add(gates.H(1)) c1.add(gates.H(2)) c2 = Circuit(4, accelerators) c2.add(gates.CNOT(0, 1)) c2.add(gates.CZ(2, 3)) c3 = c1 + c2 c = Circuit(4, accelerators) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.H(2)) c.add(gates.CNOT(0, 1)) c.add(gates.CZ(2, 3)) K.assert_allclose(c3(), c())
def test_controlled_x_vs_cnot(backend): c1 = Circuit(3) c1.add(gates.X(0)) c1.add(gates.X(2).controlled_by(0)) c2 = Circuit(3) c2.add(gates.X(0)) c2.add(gates.CNOT(0, 2)) K.assert_allclose(c1(), c2())
def test_circuit_with_noise_gates(): c = Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1), gates.CNOT(0, 1)]) noisy_c = c.with_noise((0.1, 0.2, 0.3)) assert noisy_c.depth == 4 assert noisy_c.ngates == 7 for i in [1, 3, 5, 6]: assert noisy_c.queue[i].__class__.__name__ == "PauliNoiseChannel"
def test_controlled_execution(ndevices): original_backend = qibo.get_backend() qibo.set_backend("custom") devices = {"/GPU:0": ndevices} dist_c = models.DistributedCircuit(4, devices) dist_c.add((gates.H(i) for i in range(dist_c.nglobal, 4))) dist_c.add(gates.CNOT(0, 2)) c = models.Circuit(4) c.add((gates.H(i) for i in range(dist_c.nglobal, 4))) c.add(gates.CNOT(0, 2)) initial_state = utils.random_numpy_state(c.nqubits) final_state = dist_c(np.copy(initial_state)).numpy() target_state = c(np.copy(initial_state)).numpy() np.testing.assert_allclose(target_state, final_state) qibo.set_backend(original_backend)
def test_circuit_on_qubits_execution(backend, accelerators, distribute_small): if distribute_small: smallc = Circuit(3, accelerators=accelerators) else: smallc = Circuit(3) smallc.add((gates.RX(i, theta=i + 0.1) for i in range(3))) smallc.add((gates.CNOT(0, 1), gates.CZ(1, 2))) largec = Circuit(6, accelerators=accelerators) largec.add((gates.RY(i, theta=i + 0.2) for i in range(0, 6, 2))) largec.add(smallc.on_qubits(1, 3, 5)) targetc = Circuit(6) targetc.add((gates.RY(i, theta=i + 0.2) for i in range(0, 6, 2))) targetc.add((gates.RX(i, theta=i // 2 + 0.1) for i in range(1, 6, 2))) targetc.add((gates.CNOT(1, 3), gates.CZ(3, 5))) assert largec.depth == targetc.depth K.assert_allclose(largec(), targetc())
def test_circuit_decompose_execution(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) K.assert_allclose(c(), decomp_c(), atol=1e-6)
def test_entropy_in_distributed_circuit(backend, accelerators, gateconf, target_entropy): """Check that various entropy configurations work in distributed circuit.""" 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() K.assert_allclose(final_state, target_state) K.assert_allclose(entropy[:], target_entropy, atol=_atol)
def test_cnot(backend, applyx): if applyx: gatelist = [gates.X(0)] else: gatelist = [] gatelist.append(gates.CNOT(0, 1)) final_state = apply_gates(gatelist, nqubits=2) target_state = np.zeros_like(final_state) target_state[3 * int(applyx)] = 1.0 K.assert_allclose(final_state, target_state)
def test_circuit_with_noise_gates(): """Check that ``circuit.with_noise()`` adds the proper noise channels.""" c = models.Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1), gates.CNOT(0, 1)]) noisy_c = c.with_noise((0.1, 0.2, 0.3)) assert noisy_c.depth == 4 assert noisy_c.ngates == 7 for i in [1, 3, 5, 6]: assert noisy_c.queue[i].__class__.__name__ == "PauliNoiseChannel"
def test_fuse_circuit_two_qubit_gates(backend): """Check circuit fusion in circuit with two-qubit gates only.""" c = Circuit(2) c.add(gates.CNOT(0, 1)) c.add(gates.RX(0, theta=0.1234).controlled_by(1)) c.add(gates.SWAP(0, 1)) c.add(gates.fSim(1, 0, theta=0.1234, phi=0.324)) c.add(gates.RY(1, theta=0.1234).controlled_by(0)) fused_c = c.fuse() K.assert_allclose(fused_c(), c())
def test_pauli_error(backend, density_matrix, nshots): pauli = PauliError(0, 0.2, 0.3) noise = NoiseModel() noise.add(pauli, gates.X, 1) noise.add(pauli, gates.CNOT) noise.add(pauli, gates.Z, (0,1)) circuit = Circuit(3, density_matrix=density_matrix) circuit.add(gates.CNOT(0,1)) circuit.add(gates.Z(1)) circuit.add(gates.X(1)) circuit.add(gates.X(2)) circuit.add(gates.Z(2)) circuit.add(gates.M(0, 1, 2)) target_circuit = Circuit(3, density_matrix=density_matrix) target_circuit.add(gates.CNOT(0,1)) target_circuit.add(gates.PauliNoiseChannel(0, 0, 0.2, 0.3)) target_circuit.add(gates.PauliNoiseChannel(1, 0, 0.2, 0.3)) target_circuit.add(gates.Z(1)) target_circuit.add(gates.PauliNoiseChannel(1, 0, 0.2, 0.3)) target_circuit.add(gates.X(1)) target_circuit.add(gates.PauliNoiseChannel(1, 0, 0.2, 0.3)) target_circuit.add(gates.X(2)) target_circuit.add(gates.Z(2)) target_circuit.add(gates.M(0, 1, 2)) initial_psi = random_density_matrix(3) if density_matrix else random_state(3) np.random.seed(123) K.set_seed(123) final_state = noise.apply(circuit)(initial_state=np.copy(initial_psi), nshots=nshots) final_state_samples = final_state.samples() if nshots else None np.random.seed(123) K.set_seed(123) target_final_state = target_circuit(initial_state=np.copy(initial_psi), nshots=nshots) target_final_state_samples = target_final_state.samples() if nshots else None if nshots is None: K.assert_allclose(final_state, target_final_state) else: K.assert_allclose(final_state_samples, target_final_state_samples)
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)
def test_circuit_addition_result(backend, accelerators): """Check if circuit addition works properly on Tensorflow circuit.""" original_backend = qibo.get_backend() qibo.set_backend(backend) c1 = Circuit(2, accelerators) c1.add(gates.H(0)) c1.add(gates.H(1)) c2 = Circuit(2, accelerators) c2.add(gates.CNOT(0, 1)) c3 = c1 + c2 c = Circuit(2, accelerators) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.CNOT(0, 1)) np.testing.assert_allclose(c3.execute().numpy(), c.execute().numpy()) qibo.set_backend(original_backend)
def test_circuit_unitary(backend): from qibo import matrices c = Circuit(2) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.CNOT(0, 1)) c.add(gates.X(0)) c.add(gates.Y(1)) h = np.kron(matrices.H, matrices.H) target_matrix = np.kron(matrices.X, matrices.Y) @ matrices.CNOT @ h K.assert_allclose(c.unitary(), target_matrix)