def test_vqc(method, options, compile, filename): """Performs a VQE circuit minimization test.""" def myloss(parameters, circuit, target): circuit.set_parameters(parameters) state = circuit().tensor return 1 - np.abs(np.dot(np.conj(target), state)) nqubits = 6 nlayers = 4 # Create variational circuit c = models.Circuit(nqubits) for l in range(nlayers): c.add((gates.RY(q, theta=0) for q in range(nqubits))) c.add((gates.CZ(q, q+1) for q in range(0, nqubits-1, 2))) c.add((gates.RY(q, theta=0) for q in range(nqubits))) c.add((gates.CZ(q, q+1) for q in range(1, nqubits-2, 2))) c.add(gates.CZ(0, nqubits-1)) c.add((gates.RY(q, theta=0) for q in range(nqubits))) # Optimize starting from a random guess for the variational parameters np.random.seed(0) x0 = np.random.uniform(0, 2*np.pi, 2*nqubits*nlayers + nqubits) data = np.random.normal(0, 1, size=2**nqubits) # perform optimization best, params, _ = optimize(myloss, x0, args=(c, data), method=method, options=options, compile=compile) if filename is not None: utils.assert_regression_fixture(params, filename)
def test_vqe(method, options, compile, filename): """Performs a VQE circuit minimization test.""" import qibo original_backend = qibo.get_backend() if method == "sgd" or compile: qibo.set_backend("matmuleinsum") else: qibo.set_backend("custom") original_threads = get_threads() if method == 'parallel_L-BFGS-B': if 'GPU' in qibo.get_device(): # pragma: no cover pytest.skip("unsupported configuration") qibo.set_threads(1) nqubits = 6 layers = 4 circuit = Circuit(nqubits) for l in range(layers): for q in range(nqubits): circuit.add(gates.RY(q, theta=1.0)) for q in range(0, nqubits - 1, 2): circuit.add(gates.CZ(q, q + 1)) for q in range(nqubits): circuit.add(gates.RY(q, theta=1.0)) for q in range(1, nqubits - 2, 2): circuit.add(gates.CZ(q, q + 1)) circuit.add(gates.CZ(0, nqubits - 1)) for q in range(nqubits): circuit.add(gates.RY(q, theta=1.0)) hamiltonian = XXZ(nqubits=nqubits) np.random.seed(0) initial_parameters = np.random.uniform(0, 2 * np.pi, 2 * nqubits * layers + nqubits) v = VQE(circuit, hamiltonian) best, params = v.minimize(initial_parameters, method=method, options=options, compile=compile) if method == "cma": # remove `outcmaes` folder import shutil shutil.rmtree("outcmaes") if filename is not None: utils.assert_regression_fixture(params, filename) qibo.set_backend(original_backend) qibo.set_threads(original_threads)
def test_set_parameters_with_gate_fusion(backend, trainable): """Check updating parameters of fused circuit.""" params = np.random.random(9) c = Circuit(5) c.add(gates.RX(0, theta=params[0], trainable=trainable)) c.add(gates.RY(1, theta=params[1])) c.add(gates.CZ(0, 1)) c.add(gates.RX(2, theta=params[2])) c.add(gates.RY(3, theta=params[3], trainable=trainable)) c.add(gates.fSim(2, 3, theta=params[4], phi=params[5])) c.add(gates.RX(4, theta=params[6])) c.add(gates.RZ(0, theta=params[7], trainable=trainable)) c.add(gates.RZ(1, theta=params[8])) fused_c = c.fuse() final_state = fused_c() target_state = c() K.assert_allclose(final_state, target_state) if trainable: new_params = np.random.random(9) new_params_list = list(new_params[:4]) new_params_list.append((new_params[4], new_params[5])) new_params_list.extend(new_params[6:]) else: new_params = np.random.random(9) new_params_list = list(new_params[1:3]) new_params_list.append((new_params[4], new_params[5])) new_params_list.append(new_params[6]) new_params_list.append(new_params[8]) c.set_parameters(new_params_list) fused_c.set_parameters(new_params_list) K.assert_allclose(c(), fused_c())
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}