From 907e455d46b3726053680fa565ef1e586900bf54 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Tue, 5 Oct 2021 02:08:47 +0800 Subject: [PATCH 1/5] Update circuit.py --- paddle_quantum/circuit.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/paddle_quantum/circuit.py b/paddle_quantum/circuit.py index 2eba39f..dc4e8b9 100644 --- a/paddle_quantum/circuit.py +++ b/paddle_quantum/circuit.py @@ -59,6 +59,15 @@ def __init__(self, n): paddle.to_tensor(np.array([math.pi / 4])), paddle.to_tensor(np.array([-math.pi / 4]))] # Record history of adding gates to the circuit self.__history = [] + + def expand(self,new_n): + """ + 为原来的量子电路进行比特数扩展 + + Args: + new_n(int):扩展后的量子比特数 + """ + self.n = new_n def __add__(self, cir): r"""重载加法 ‘+’ 运算符,用于拼接两个维度相同的电路 From ebfb93221f8c87742c810ab5f835761767327e04 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Tue, 5 Oct 2021 02:37:45 +0800 Subject: [PATCH 2/5] Create test.py --- test_and_documents/test.py | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 test_and_documents/test.py diff --git a/test_and_documents/test.py b/test_and_documents/test.py new file mode 100644 index 0000000..db7e2be --- /dev/null +++ b/test_and_documents/test.py @@ -0,0 +1,80 @@ +import math +import numpy as np +import paddle +from paddle import matmul, transpose, trace +from paddle_quantum.circuit import UAnsatz +from paddle_quantum.utils import dagger, random_pauli_str_generator, pauli_str_to_matrix +from paddle_quantum.state import vec, vec_random, density_op, density_op_random +theta_size = 4 +num_qubits = 2 +ITR = 80 +LR=0.5 +SEED = 1 +paddle.seed(SEED) +path = '/home/aistudio/飞桨常规赛:量子电路合成/Question_2_Unitary.txt' + + +#普通构建方法 +def U_theta(theta,num_qubits): + cir = UAnsatz(num_qubits) + cir.ry(theta[0],0) + cir.ry(theta[1],1) + cir.cnot([0,1]) + cir.ry(theta[2],0) + cir.ry(theta[3],1) + return cir.U + +#通过量子比特扩展 +def U_theta2(theta,num_qubits): + cir = UAnsatz(1) + cir.ry(theta[0],0) + cir.expand(num_qubits) + cir.ry(theta[1],1) + cir.cnot([0,1]) + cir.ry(theta[2],0) + cir.ry(theta[3],1) + return cir.U + +class Optimization_exl(paddle.nn.Layer): + def __init__(self,shape,dtype='float64'): + super(Optimization_exl,self).__init__() + f = np.loadtxt(path) + self.u = paddle.to_tensor(f) + self.theta = self.create_parameter(shape=shape, + default_initializer = paddle.nn.initializer.Uniform(low=0,high=2*np.pi), + dtype=dtype,is_bias=False) + + def forward(self): + #U = U_theta(self.theta,num_qubits) + U = U_theta2(self.theta,num_qubits) + U_dagger = dagger(U) + loss = 1-(0.25*paddle.real(trace(matmul(self.u,U_dagger)))[0]) + return loss + + def result(self): + return self.theta + +loss_list = [] +parameter_list = [] +myLayer = Optimization_exl([theta_size]) +opt = paddle.optimizer.Adam(learning_rate=LR,parameters=myLayer.parameters()) + +for itr in range(ITR): + loss = myLayer()[0] + loss.backward() + opt.minimize(loss) + opt.clear_grad() + + loss_list.append(loss.numpy()[0]) + parameter_list.append(myLayer.parameters()[0].numpy()) + #if itr % 5 == 0: + #print('iter:', itr, ' loss: %.4f' % loss.numpy()) +print(myLayer.result()) +""" +U_theta 结果 +Tensor(shape=[4], dtype=float64, place=CPUPlace, stop_gradient=False, + [-0.43797367, 7.05269038, 3.48305136, 0.42135362]) +U_theta2 结果 +Tensor(shape=[4], dtype=float64, place=CPUPlace, stop_gradient=False, + [-0.43797367, 7.05269038, 3.48305136, 0.42135362]) +""" From 06120cc800c5db95d2c9c3ee89a2a674e3244acb Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Tue, 5 Oct 2021 02:47:19 +0800 Subject: [PATCH 3/5] Create readme.md --- test_and_documents/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 test_and_documents/readme.md diff --git a/test_and_documents/readme.md b/test_and_documents/readme.md new file mode 100644 index 0000000..f8d685c --- /dev/null +++ b/test_and_documents/readme.md @@ -0,0 +1 @@ +通过在UAnsatz类中添加新的成员函数expand来实现扩展 From 7f4ee1c8be5a269e7883a6a710aedb026c716e54 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Wed, 27 Oct 2021 13:56:10 +0800 Subject: [PATCH 4/5] Update circuit.py --- paddle_quantum/circuit.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/paddle_quantum/circuit.py b/paddle_quantum/circuit.py index dc4e8b9..4e5c627 100644 --- a/paddle_quantum/circuit.py +++ b/paddle_quantum/circuit.py @@ -26,7 +26,7 @@ from paddle_quantum.utils import partial_trace, dagger, pauli_str_to_matrix from paddle_quantum import shadow from paddle_quantum.intrinsic import * -from paddle_quantum.state import density_op +from paddle_quantum.state import density_op,vec __all__ = [ "UAnsatz", @@ -67,8 +67,22 @@ def expand(self,new_n): Args: new_n(int):扩展后的量子比特数 """ + assert new_n>=self.n,'扩展后量子比特数要大于原量子比特数' + diff = new_n-self.n + dim = 2**diff + if self.__state is not None: + if self.__run_mode=='density_matrix': + shape = (dim,dim) + _state = paddle.to_tensor(density_op(diff)) + elif self.__run_mode=='state_vector': + shape = (dim,) + _state = paddle.to_tensor(vec(0,diff)) + + _state= paddle.reshape(_state,shape) + _state = kron(self.__state,_state) + self.__state = _state self.n = new_n - + def __add__(self, cir): r"""重载加法 ‘+’ 运算符,用于拼接两个维度相同的电路 From f9f04245390e4a5c3004b2c7935f78081e71fa80 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Wed, 27 Oct 2021 13:56:58 +0800 Subject: [PATCH 5/5] Update test.py --- test_and_documents/test.py | 98 ++++++++++---------------------------- 1 file changed, 26 insertions(+), 72 deletions(-) diff --git a/test_and_documents/test.py b/test_and_documents/test.py index db7e2be..9d998d8 100644 --- a/test_and_documents/test.py +++ b/test_and_documents/test.py @@ -1,80 +1,34 @@ -import math -import numpy as np -import paddle -from paddle import matmul, transpose, trace from paddle_quantum.circuit import UAnsatz -from paddle_quantum.utils import dagger, random_pauli_str_generator, pauli_str_to_matrix -from paddle_quantum.state import vec, vec_random, density_op, density_op_random -theta_size = 4 -num_qubits = 2 -ITR = 80 -LR=0.5 -SEED = 1 -paddle.seed(SEED) -path = '/home/aistudio/飞桨常规赛:量子电路合成/Question_2_Unitary.txt' - +from paddle import kron +from paddle_quantum.state import vec,density_op +import paddle -#普通构建方法 -def U_theta(theta,num_qubits): - cir = UAnsatz(num_qubits) - cir.ry(theta[0],0) - cir.ry(theta[1],1) - cir.cnot([0,1]) - cir.ry(theta[2],0) - cir.ry(theta[3],1) - return cir.U -#通过量子比特扩展 -def U_theta2(theta,num_qubits): +#density_matrix +def test_density_matrix(): cir = UAnsatz(1) - cir.ry(theta[0],0) - cir.expand(num_qubits) - cir.ry(theta[1],1) - cir.cnot([0,1]) - cir.ry(theta[2],0) - cir.ry(theta[3],1) - return cir.U + cir.ry(paddle.to_tensor(1,dtype='float64'),0) + state = cir.run_density_matrix() + cir.expand(3) + print(cir.get_state()) -class Optimization_exl(paddle.nn.Layer): - def __init__(self,shape,dtype='float64'): - super(Optimization_exl,self).__init__() - f = np.loadtxt(path) - self.u = paddle.to_tensor(f) - self.theta = self.create_parameter(shape=shape, - default_initializer = paddle.nn.initializer.Uniform(low=0,high=2*np.pi), - dtype=dtype,is_bias=False) - - def forward(self): - #U = U_theta(self.theta,num_qubits) - U = U_theta2(self.theta,num_qubits) - U_dagger = dagger(U) - loss = 1-(0.25*paddle.real(trace(matmul(self.u,U_dagger)))[0]) - return loss - - def result(self): - return self.theta + cir2 = UAnsatz(3) + cir2.ry(paddle.to_tensor(1,dtype='float64'),0) + cir2.run_density_matrix() + print(cir2.get_state()) -loss_list = [] -parameter_list = [] -myLayer = Optimization_exl([theta_size]) -opt = paddle.optimizer.Adam(learning_rate=LR,parameters=myLayer.parameters()) +#state_vector +def test_state_vector(): + cir = UAnsatz(1) + cir.ry(paddle.to_tensor(1,dtype='float64'),0) + state = cir.run_state_vector() + cir.expand(3) + print(cir.get_state()) -for itr in range(ITR): - loss = myLayer()[0] - loss.backward() - opt.minimize(loss) - opt.clear_grad() + cir2 = UAnsatz(3) + cir2.ry(paddle.to_tensor(1,dtype='float64'),0) + cir2.run_state_vector() + print(cir2.get_state()) - loss_list.append(loss.numpy()[0]) - parameter_list.append(myLayer.parameters()[0].numpy()) - #if itr % 5 == 0: - #print('iter:', itr, ' loss: %.4f' % loss.numpy()) -print(myLayer.result()) -""" -U_theta 结果 -Tensor(shape=[4], dtype=float64, place=CPUPlace, stop_gradient=False, - [-0.43797367, 7.05269038, 3.48305136, 0.42135362]) -U_theta2 结果 -Tensor(shape=[4], dtype=float64, place=CPUPlace, stop_gradient=False, - [-0.43797367, 7.05269038, 3.48305136, 0.42135362]) -""" +test_density_matrix() +test_state_vector()