From 7de8de337f1cc02af845e9c2d4749c8248024cca Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Mon, 31 Jul 2023 16:36:18 +0800 Subject: [PATCH 1/2] fix label missing of zero-depth qubit --- src/quafu/visualisation/circuitPlot.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/quafu/visualisation/circuitPlot.py b/src/quafu/visualisation/circuitPlot.py index c5c7d1e..b5d0f18 100644 --- a/src/quafu/visualisation/circuitPlot.py +++ b/src/quafu/visualisation/circuitPlot.py @@ -1,3 +1,17 @@ +# (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 matplotlib.patheffects as pe import matplotlib.pyplot as plt import numpy as np @@ -7,7 +21,7 @@ from quafu.elements.quantum_element import Instruction, ControlledGate -# the following line for developers only +# this line for developers only # from quafu.circuits.quantum_circuit import QuantumCircuit line_args = {} @@ -98,8 +112,8 @@ def __init__(self, qc): for gate in qc.gates: assert isinstance(gate, Instruction) self._process_ins(gate) - qubits_used = self.dorders > 0 - self.used_qbit_num = qc.num + qubits_used = qc.used_qubits + self.used_qbit_num = len(qubits_used) # TODO: drop off unused-qubits # self.used_qbit_num = np.sum(qubits_used) @@ -109,8 +123,8 @@ def __init__(self, qc): self._proc_measure(self.depth - 1, q) # step2: initialize bit-label - self.q_label = {i: r'$|q_{%d}\rangle$' % i for i in range(qc.num) if qubits_used[i]} - self.c_label = {iq: f'c_{ic}' for iq, ic in qc.measures.items() if qubits_used[iq]} + self.q_label = {i: r'$|q_{%d}\rangle$' % i for i in range(qc.num) if i in qubits_used} + self.c_label = {iq: f'c_{ic}' for iq, ic in qc.measures.items() if iq in qubits_used} # step3: figure coordination self.xs = np.arange(-3 / 2, self.depth + 3 / 2) @@ -144,6 +158,7 @@ def __call__(self, title, size=30, ha='center', va='baseline') + print(title) self._text_list.append(title) # initialize a figure From 95f07140c2b62b31b5e53e30f15030ab99ab175a Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Mon, 31 Jul 2023 16:36:51 +0800 Subject: [PATCH 2/2] add an example of variation --- .../benchmark/variational/ladder_circuit.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/quafu/benchmark/variational/ladder_circuit.py diff --git a/src/quafu/benchmark/variational/ladder_circuit.py b/src/quafu/benchmark/variational/ladder_circuit.py new file mode 100644 index 0000000..297326e --- /dev/null +++ b/src/quafu/benchmark/variational/ladder_circuit.py @@ -0,0 +1,31 @@ +from quafu.circuits.quantum_circuit import QuantumCircuit +from numpy import random + + +# number of qubits, number of layers +bit_num, n_layers = 4, 2 + + +def ladder_layout_circuit(params, pbc=False): + """ + `params` is for circuit trainable parameters + """ + qc = QuantumCircuit(bit_num) + offset = 0 if pbc else 1 + for j in range(n_layers): + for i in range(bit_num - offset): + qc.cnot(i, (i + 1) % bit_num) + for i in range(bit_num): + qc.rx(i, params[j, i]) + return qc + + +def plot(): + para = random.random((n_layers, bit_num)) + qc = ladder_layout_circuit(para) + qc.plot_circuit(title='Ladder Layout for \nVariational Circuit', + show=True, + save=False, + ) + +plot() \ No newline at end of file