def test_add_gate_in_parametric_circuit(self): from qulacs import ParametricQuantumCircuit from qulacs.gate import X circuit = ParametricQuantumCircuit(1) gate = X(0) circuit.add_gate(gate) del gate s = circuit.to_string() del circuit
def create_initial_output_gate(self): """output用ゲートU_outの組み立て&パラメータ初期値の設定""" u_out = ParametricQuantumCircuit(self.nqubit) time_evol_gate = create_time_evol_gate(self.nqubit) theta = 2.0 * np.pi * np.random.rand(self.c_depth, self.nqubit, 3) self.theta = theta.flatten() for d in range(self.c_depth): u_out.add_gate(time_evol_gate) for i in range(self.nqubit): u_out.add_parametric_RX_gate(i, theta[d, i, 0]) u_out.add_parametric_RZ_gate(i, theta[d, i, 1]) u_out.add_parametric_RX_gate(i, theta[d, i, 2]) self.output_gate = u_out
def create_initial_output_gate(self): """Output gate U_out and parameter setting""" u_out = ParametricQuantumCircuit(self.nqubit) if self.time_evol_hamiltonian_type == 'Ising': time_evol_gate = create_Ising_time_evol_gate(self.nqubit) else: time_evol_gate = create_Heisenberg_time_evol_gate(self.nqubit) theta = 2.0 * np.pi * np.random.rand(self.c_depth, self.nqubit, 3) self.theta = theta.flatten() for d in range(self.c_depth): u_out.add_gate(time_evol_gate) for i in range(self.nqubit): u_out.add_parametric_RX_gate(i, theta[d, i, 0]) u_out.add_parametric_RZ_gate(i, theta[d, i, 1]) u_out.add_parametric_RX_gate(i, theta[d, i, 2]) self.output_gate = u_out
def test_VqeOptimizer(): from qulacs import ParametricQuantumCircuit from qulacs import QuantumState from qulacs import Observable from qulacs.gate import Probabilistic, X, Y, Z import numpy as np import matplotlib.pyplot as plt n_qubit = 2 p_list = [0.05, 0.1, 0.15] parametric_circuit_list = \ [ParametricQuantumCircuit(n_qubit) for i in range(len(p_list))] initial_state = QuantumState(n_qubit) for (p, circuit) in zip(p_list, parametric_circuit_list): circuit.add_H_gate(0) circuit.add_parametric_RY_gate(1, np.pi / 6) circuit.add_CNOT_gate(0, 1) prob = Probabilistic([p / 4, p / 4, p / 4], [X(0), Y(0), Z(0)]) circuit.add_gate(prob) noiseless_circuit = ParametricQuantumCircuit(n_qubit) noiseless_circuit.add_H_gate(0) noiseless_circuit.add_parametric_RY_gate(1, np.pi / 6) noiseless_circuit.add_CNOT_gate(0, 1) n_sample_per_circuit = 1 n_circuit_sample = 1000 obs = Observable(n_qubit) obs.add_operator(1.0, "Z 0 Z 1") obs.add_operator(0.5, "X 0 X 1") initial_param = np.array([np.pi / 6]) opt = VqeOptimizer(parametric_circuit_list, initial_state, obs, initial_param, p_list, n_circuit_sample=n_circuit_sample, n_sample_per_circuit=n_sample_per_circuit, noiseless_circuit=noiseless_circuit) noisy = opt.sample_output(initial_param) mitigated, exp_array, _ = opt.sample_mitigated_output(initial_param, return_full=True) exact = opt.exact_output(initial_param) print(noisy, exact) print(exp_array, mitigated) opt_param = opt.optimize() print(opt_param) theta_list = np.linspace(0, np.pi, 100) output_list = [opt.exact_output([theta]) for theta in theta_list] plt.plot(theta_list, output_list, color="black", linestyle="dashed", label="exact") plt.scatter(opt.parameter_history, opt.exp_history, c="blue", label="optimization history") plt.xlabel("theta") plt.ylabel("output") plt.legend() plt.show()
def test_parametric_gate_position(self): from qulacs import ParametricQuantumCircuit, QuantumState from qulacs.gate import ParametricRX def check(pqc, idlist): cnt = pqc.get_parameter_count() self.assertEqual(cnt, len(idlist)) for ind in range(cnt): pos = pqc.get_parametric_gate_position(ind) self.assertEqual(pos, idlist[ind]) pqc = ParametricQuantumCircuit(1) gate = ParametricRX(0, 0.1) pqc.add_parametric_gate(gate) # [0] check(pqc, [0]) pqc.add_parametric_gate(gate) # [0, 1] check(pqc, [0, 1]) pqc.add_gate(gate) # [0, 1, *] check(pqc, [0, 1]) pqc.add_parametric_gate(gate, 0) # [2, 0, 1, *] check(pqc, [1, 2, 0]) pqc.add_gate(gate, 0) # [*, 2, 0, 1, *] check(pqc, [2, 3, 1]) pqc.add_parametric_gate(gate, 0) # [3, *, 2, 0, 1, *] check(pqc, [3, 4, 2, 0]) pqc.remove_gate(4) # [2, *, 1, 0, *] check(pqc, [3, 2, 0]) pqc.remove_gate(1) # [2, 1, 0, *] check(pqc, [2, 1, 0]) pqc.add_parametric_gate(gate) # [2, 1, 0, *, 3] check(pqc, [2, 1, 0, 4]) pqc.add_parametric_gate(gate, 2) # [2, 1, 4, 0, *, 3] check(pqc, [3, 1, 0, 5, 2]) pqc.remove_gate(3) # [1, 0, 3, *, 2] check(pqc, [1, 0, 4, 2])
def f(): circuit = ParametricQuantumCircuit(1) gate = ParametricRX(0, 0.1) circuit.add_parametric_gate(gate) circuit.add_parametric_gate(gate) circuit.add_gate(gate) gate.set_parameter_value(0.2) circuit.add_parametric_gate(gate) circuit.add_parametric_RX_gate(0, 0.3) gate2 = gate.copy() gate2.set_parameter_value(0.4) gate.set_parameter_value(1.0) del gate circuit.add_parametric_gate(gate2) circuit.remove_gate(1) del gate2 return circuit
def test_circuit_add_parametric_gate(self): from qulacs import ParametricQuantumCircuit, QuantumState from qulacs.gate import Identity, X, Y, Z, H, S, Sdag, T, Tdag, sqrtX, sqrtXdag, sqrtY, sqrtYdag from qulacs.gate import P0, P1, U1, U2, U3, RX, RY, RZ, CNOT, CZ, SWAP, TOFFOLI, FREDKIN, Pauli, PauliRotation from qulacs.gate import DenseMatrix, SparseMatrix, DiagonalMatrix, RandomUnitary, ReversibleBoolean, StateReflection from qulacs.gate import BitFlipNoise, DephasingNoise, IndependentXZNoise, DepolarizingNoise, TwoQubitDepolarizingNoise, AmplitudeDampingNoise, Measurement from qulacs.gate import merge, add, to_matrix_gate, Probabilistic, CPTP, Instrument, Adaptive from qulacs.gate import ParametricRX, ParametricRY, ParametricRZ, ParametricPauliRotation from scipy.sparse import lil_matrix qc = ParametricQuantumCircuit(3) qs = QuantumState(3) ref = QuantumState(3) sparse_mat = lil_matrix((4, 4)) sparse_mat[0, 0] = 1 sparse_mat[1, 1] = 1 def func(v, d): return (v + 1) % d def adap(v): return True gates = [ Identity(0), X(0), Y(0), Z(0), H(0), S(0), Sdag(0), T(0), Tdag(0), sqrtX(0), sqrtXdag(0), sqrtY(0), sqrtYdag(0), Probabilistic([0.5, 0.5], [X(0), Y(0)]), CPTP([P0(0), P1(0)]), Instrument([P0(0), P1(0)], 1), Adaptive(X(0), adap), CNOT(0, 1), CZ(0, 1), SWAP(0, 1), TOFFOLI(0, 1, 2), FREDKIN(0, 1, 2), Pauli([0, 1], [1, 2]), PauliRotation([0, 1], [1, 2], 0.1), DenseMatrix(0, np.eye(2)), DenseMatrix([0, 1], np.eye(4)), SparseMatrix([0, 1], sparse_mat), DiagonalMatrix([0, 1], np.ones(4)), RandomUnitary([0, 1]), ReversibleBoolean([0, 1], func), StateReflection(ref), BitFlipNoise(0, 0.1), DephasingNoise(0, 0.1), IndependentXZNoise(0, 0.1), DepolarizingNoise(0, 0.1), TwoQubitDepolarizingNoise(0, 1, 0.1), AmplitudeDampingNoise(0, 0.1), Measurement(0, 1), merge(X(0), Y(1)), add(X(0), Y(1)), to_matrix_gate(X(0)), P0(0), P1(0), U1(0, 0.), U2(0, 0., 0.), U3(0, 0., 0., 0.), RX(0, 0.), RY(0, 0.), RZ(0, 0.), ] gates.append(merge(gates[0], gates[1])) gates.append(add(gates[0], gates[1])) parametric_gates = [ ParametricRX(0, 0.1), ParametricRY(0, 0.1), ParametricRZ(0, 0.1), ParametricPauliRotation([0, 1], [1, 1], 0.1) ] ref = None for gate in gates: qc.add_gate(gate) for gate in gates: qc.add_gate(gate) for pgate in parametric_gates: qc.add_parametric_gate(pgate) for pgate in parametric_gates: qc.add_parametric_gate(pgate) qc.update_quantum_state(qs) qc = None qs = None for gate in gates: gate = None for pgate in parametric_gates: gate = None gates = None parametric_gates = None
input_train.append(d[0]) value_train.append(d[1]) for d in test_data: input_test.append(d[0]) value_test.append(d[1]) ## Basic gates I_mat = np.eye(2, dtype=complex) X_mat = X(0).get_matrix() Z_mat = Z(0).get_matrix() # Construct an output gate U_out and initialization. U_out = ParametricQuantumCircuit(nqubit) for d in range(c_depth): for i in range(nqubit): angle = 2.0 * np.pi * np.random.rand() U_out.add_parametric_RX_gate(i,angle) angle = 2.0 * np.pi * np.random.rand() U_out.add_parametric_RZ_gate(i,angle) angle = 2.0 * np.pi * np.random.rand() U_out.add_parametric_RX_gate(i,angle) meas0 = Measurement(0, 0) U_out.add_gate(meas0) meas1 = Measurement(1, 1) U_out.add_gate(meas1) #meas2 = Measurement(2, 2) #U_out.add_gate(meas2)