From cc59cd3385ec9405a2867f1fc05b7a0350e0fd97 Mon Sep 17 00:00:00 2001 From: qtzhuang Date: Tue, 5 Mar 2024 17:41:56 +0800 Subject: [PATCH] test: test merging encoding_layer and entangle_layer --- quafu/algorithms/templates/basic_entangle.py | 1 - tests/quafu/algorithms/merge_circuits_test.py | 40 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/quafu/algorithms/merge_circuits_test.py diff --git a/quafu/algorithms/templates/basic_entangle.py b/quafu/algorithms/templates/basic_entangle.py index 72c087c..8f66dbb 100644 --- a/quafu/algorithms/templates/basic_entangle.py +++ b/quafu/algorithms/templates/basic_entangle.py @@ -82,7 +82,6 @@ def _build(self): Parameter("theta_%d" % j, np.round(np.random.rand(), 3)) for j in range(repeat * self.num_qubits) ] - print(theta) for layer in range(repeat): j = layer * self.num_qubits for i in range(self.num_qubits): diff --git a/tests/quafu/algorithms/merge_circuits_test.py b/tests/quafu/algorithms/merge_circuits_test.py new file mode 100644 index 0000000..5af7f48 --- /dev/null +++ b/tests/quafu/algorithms/merge_circuits_test.py @@ -0,0 +1,40 @@ +# (C) Copyright 2023 Beijing Academy of Quantum Information Sciences +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import numpy as np +from quafu.algorithms import AmplitudeEmbedding, AngleEmbedding +from quafu.algorithms.ansatz import QuantumNeuralNetwork +from quafu.algorithms.templates.basic_entangle import BasicEntangleLayers +from quafu.circuits.quantum_circuit import QuantumCircuit + + +class TestMergeCircuits: + """Example of merging circuits""" + + def test_merge_circuits(self): + state = np.array([7, 2, 3, 4]) + encoding_layer = AmplitudeEmbedding(state=state, num_qubits=2, normalize=True) + + # feature = np.array([[6, -12.5], [8, 9.5], [5, 0.5]]) + # encoding_layer = AngleEmbedding(features=feature, num_qubits=2, rotation="Y") + + weights = np.array([[-0.850, 1.287], [0.871, 0.184]]) + entangle_layer = BasicEntangleLayers(weights=weights, num_qubits=2) + + # entangle_layer2 = BasicEntangleLayers(num_qubits=2, repeat=3) + + circuit = encoding_layer + entangle_layer + + qnn = QuantumNeuralNetwork(2, circuit) + + qnn.draw_circuit(width=2)