Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix label missing of only-measured qubits #45

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/quafu/benchmark/variational/ladder_circuit.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 20 additions & 5 deletions src/quafu/visualisation/circuitPlot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -144,6 +158,7 @@ def __call__(self,
title,
size=30,
ha='center', va='baseline')
print(title)
self._text_list.append(title)

# initialize a figure
Expand Down