From 969d82af45d3cb51bacb74a439cf167d8e77d892 Mon Sep 17 00:00:00 2001 From: wjz Date: Thu, 12 Jun 2025 12:10:15 +0800 Subject: [PATCH 1/2] feat: Newly added support for the condx gate --- cqlib/circuits/circuit.py | 9 ++++++ cqlib/circuits/gates/__init__.py | 2 ++ cqlib/circuits/gates/condx.py | 52 ++++++++++++++++++++++++++++++++ docs/guides/02-circuit.ipynb | 4 ++- tests/circuit/test_circuit.py | 8 +++-- 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 cqlib/circuits/gates/condx.py diff --git a/cqlib/circuits/circuit.py b/cqlib/circuits/circuit.py index 4d43063..78b2463 100644 --- a/cqlib/circuits/circuit.py +++ b/cqlib/circuits/circuit.py @@ -422,6 +422,15 @@ class Circuit: """ self.append(gates.CRZ(theta), [control_qubit, target_qubit]) + def condx(self, qubit: IntQubit): + """ + Applies the condex gate to a specified qubit. + + Args: + qubit (IntQubit): The qubit to apply the condex gate. + """ + self.append(gates.CONDX(), [qubit]) + def s(self, qubit: IntQubit): """ Applies the S gate (phase gate) to a specified qubit. diff --git a/cqlib/circuits/gates/__init__.py b/cqlib/circuits/gates/__init__.py index 893c079..c2fdfd2 100644 --- a/cqlib/circuits/gates/__init__.py +++ b/cqlib/circuits/gates/__init__.py @@ -29,8 +29,10 @@ from .x import X, CX, CNOT, CCX, CCNOT, X2P, X2M from .xy import XY, XY2M, XY2P from .y import Y, Y2M, Y2P, CY from .z import Z, CZ +from .condx import CONDX __all__ = ( + 'CONDX', 'H', 'I', 'RX', 'CRX', diff --git a/cqlib/circuits/gates/condx.py b/cqlib/circuits/gates/condx.py new file mode 100644 index 0000000..5a02079 --- /dev/null +++ b/cqlib/circuits/gates/condx.py @@ -0,0 +1,52 @@ +# This code is part of cqlib. +# +# Copyright (C) 2025 China Telecom Quantum Group, QuantumCTek Co., Ltd., +# Center for Excellence in Quantum Information and Quantum Physics. +# +# This code is licensed under the Apache License, Version 2.0. You may +# obtain a copy of this license in the LICENSE file in the root directory +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +"""Defines the CONDX gate.""" + +from cqlib.circuits.gates.gate import Gate + + +class CONDX(Gate): + """ + The CONDX gate is a measurement-based conditional operation that: + - If the previous measurement (M gate) results in |1⟩ state, applies an X gate to reset the qubit to |0⟩ state + - If the previous measurement results in |0⟩ state, performs an Idle operation, keeping the qubit in |0⟩ state + + This gate is commonly used in quantum error correction and quantum state preparation protocols + where we need to reset qubits to |0⟩ state based on measurement outcomes + """ + + def __init__(self, label: str | None = None): + """ + Initialize the CONDX gate. + + Args: + label (str | None, optional): An optional label for the CONDX instruction. + Defaults to None. + """ + # Using the base class to initialize with the necessary parameters + super().__init__('CONDX', 1, [], label=label) + + def __array__(self, dtype=None): + """ + Convert the gate to a numpy array representation. + + This method is not implemented and will raise an error if called. + + Args: + dtype: The desired data type for the array (optional). + + Raises: + NotImplementedError: This method is not implemented yet. + """ + raise NotImplementedError("Not implemented yet") diff --git a/docs/guides/02-circuit.ipynb b/docs/guides/02-circuit.ipynb index fddab4f..fd392c7 100644 --- a/docs/guides/02-circuit.ipynb +++ b/docs/guides/02-circuit.ipynb @@ -69,7 +69,8 @@ "|CZ | $CZ =\\left[ \\begin{array}{cccc}1&0&0&0 \\\\ 0&1&0&0 \\\\ 0&0&1&0 \\\\ 0&0&0&-1\\end{array}\\right]$ | CZ Q1 Q2 | Q1,Q2需满足
硬件连接条件|\n", "|RZ |$RZ(\\theta) = e^{-i\\theta/2 \\sigma_z } = \\left[\\begin{array}{cc} e^{-i\\theta/2}& 0 \\\\ 0 & e^{i\\theta/2}\\end{array}\\right]$ | RZ Q1 $\\theta$ | 无|\n", "|I | 在一段时间t(ns)内无操作 | I Q1 t | t为整数,单位为0.5ns
即当t=1时,时间为0.5ns|\n", - "|B | 对齐量子操作| B Q1 Q2 | 无|" + "|B | 对齐量子操作| B Q1 Q2 | 无|\n", + "|CONDX | 依赖测量结果的条件X操作| CONDX Q1 | 无|" ] }, { @@ -108,6 +109,7 @@ "|RX| $RX(\\theta) = e^{-i\\theta/2 \\sigma_x } = \\left[\\begin{array}{cc} \\cos\\theta/2 & -i\\sin \\theta/2 \\\\ -i\\sin\\theta/2 & \\cos \\theta/2 \\end{array}\\right]$ | RX Q1 $\\theta$ | RZ Q1 $\\pi/2$
X2P Q1
RZ Q1 $\\theta$
X2M Q1
RZ Q1 $-\\pi/2$ |\n", "|RY| $RY(\\theta) = e^{-i\\theta/2\\, \\sigma_y } = \\left[\\begin{array}{cc} \\cos\\theta/2 & -\\sin \\theta/2 \\\\ \\sin\\theta/2 & \\cos \\theta/2 \\end{array}\\right]$ | RY Q1 $\\theta$ | X2P Q1
RZ Q1 $\\theta$
X2M Q1|\n", "|RXY | $RXY(\\phi, \\theta) = e^{-i \\theta/2 \\hat{n}\\cdot\\hat{\\sigma}}$
$=\\left[\\begin{array}{cc} \\cos \\theta/2 & -ie^{-i\\phi}\\sin\\theta/2 \\\\-i e^{i\\phi} \\sin\\theta/2 & \\cos\\theta/2\\end{array}\\right]$
$\\hat{n} = (\\cos\\phi, \\sin\\phi, 0)$ |RXY Q1 $\\phi~~\\theta$ |RZ Q1 $\\pi/2 − \\phi$
X2P Q1
RZ Q1 $\\theta$
X2M Q1
RZ Q1 $\\phi-\\pi/2$|\n", + "|RST | 主动复位操作 | RST Q1 | M Q1
CONDX Q1|\n", "\n", "注:\n", "\n", diff --git a/tests/circuit/test_circuit.py b/tests/circuit/test_circuit.py index 551f977..aedb265 100644 --- a/tests/circuit/test_circuit.py +++ b/tests/circuit/test_circuit.py @@ -52,6 +52,7 @@ def test_p0(): def test_qcis_gate(): """Test QCIS gate""" circuit = Circuit(5) + circuit.condx(0) circuit.h(0) circuit.i(0, 1) circuit.rx(1, 1) @@ -79,7 +80,8 @@ def test_qcis_gate(): circuit.measure(4) assert Qubit(0) in circuit.qubits - s = """H Q0 + s = """CONDX Q0 +H Q0 I Q0 1 RX Q1 1 RY Q2 2 @@ -111,6 +113,7 @@ M Q4""" def test_all_gate(): """Test all gate""" circuit = Circuit(5) + circuit.condx(0) circuit.h(0) circuit.i(1, 2) circuit.rx(2, 1) @@ -142,7 +145,8 @@ def test_all_gate(): circuit.barrier(0, 1, 2, 3, 4) circuit.measure_all() - s = """H Q0 + s = """CONDX Q0 +H Q0 I Q1 2 RX Q2 1 CRX Q1 Q2 2 -- Gitee From f15009d6af19ddef7e3b9cedf8d310a6024c1a0b Mon Sep 17 00:00:00 2001 From: wjz Date: Tue, 17 Jun 2025 14:20:48 +0800 Subject: [PATCH 2/2] fix: condx gate --- cqlib/simulator/simple_simulator.py | 2 ++ cqlib/simulator/statevector_simulator.py | 2 ++ docs/guides/02-circuit.ipynb | 3 +-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cqlib/simulator/simple_simulator.py b/cqlib/simulator/simple_simulator.py index 66813e5..72799fb 100644 --- a/cqlib/simulator/simple_simulator.py +++ b/cqlib/simulator/simple_simulator.py @@ -162,6 +162,8 @@ class SimpleSimulator: self._mq.append(item.qubits[0].index) if instr.name in ['I', 'B', 'M']: continue + if instr.name == 'CONDX': + raise ValueError("CONDX gate is not supported.") if instr.params: ps = self.params_value_with_gradient(instr.params, self.circuit.parameters_value) if instr.name == 'RX': diff --git a/cqlib/simulator/statevector_simulator.py b/cqlib/simulator/statevector_simulator.py index 97539c3..ef7440c 100644 --- a/cqlib/simulator/statevector_simulator.py +++ b/cqlib/simulator/statevector_simulator.py @@ -137,6 +137,8 @@ class StatevectorSimulator: if item.instruction.name == 'M': self.measure_qubits.extend(qubits) continue + if item.instruction.name == 'CONDX': + raise ValueError("CONDX gate is not supported.") ps = [] for param in item.instruction.params: if isinstance(param, Parameter): diff --git a/docs/guides/02-circuit.ipynb b/docs/guides/02-circuit.ipynb index fd392c7..191b1ec 100644 --- a/docs/guides/02-circuit.ipynb +++ b/docs/guides/02-circuit.ipynb @@ -70,7 +70,7 @@ "|RZ |$RZ(\\theta) = e^{-i\\theta/2 \\sigma_z } = \\left[\\begin{array}{cc} e^{-i\\theta/2}& 0 \\\\ 0 & e^{i\\theta/2}\\end{array}\\right]$ | RZ Q1 $\\theta$ | 无|\n", "|I | 在一段时间t(ns)内无操作 | I Q1 t | t为整数,单位为0.5ns
即当t=1时,时间为0.5ns|\n", "|B | 对齐量子操作| B Q1 Q2 | 无|\n", - "|CONDX | 依赖测量结果的条件X操作| CONDX Q1 | 无|" + "|CONDX | 依赖测量结果的条件X操作。
如果之前的测量(M 门)结果为 \\\\|1⟩ 状态,则应用 X 门将量子比特重置为 \\\\|0⟩ 状态。
如果之前的测量结果为 \\\\|0⟩ 状态,则不执行操作,保持量子比特处于 \\\\|0⟩ 状态。| CONDX Q1 | 无|" ] }, { @@ -109,7 +109,6 @@ "|RX| $RX(\\theta) = e^{-i\\theta/2 \\sigma_x } = \\left[\\begin{array}{cc} \\cos\\theta/2 & -i\\sin \\theta/2 \\\\ -i\\sin\\theta/2 & \\cos \\theta/2 \\end{array}\\right]$ | RX Q1 $\\theta$ | RZ Q1 $\\pi/2$
X2P Q1
RZ Q1 $\\theta$
X2M Q1
RZ Q1 $-\\pi/2$ |\n", "|RY| $RY(\\theta) = e^{-i\\theta/2\\, \\sigma_y } = \\left[\\begin{array}{cc} \\cos\\theta/2 & -\\sin \\theta/2 \\\\ \\sin\\theta/2 & \\cos \\theta/2 \\end{array}\\right]$ | RY Q1 $\\theta$ | X2P Q1
RZ Q1 $\\theta$
X2M Q1|\n", "|RXY | $RXY(\\phi, \\theta) = e^{-i \\theta/2 \\hat{n}\\cdot\\hat{\\sigma}}$
$=\\left[\\begin{array}{cc} \\cos \\theta/2 & -ie^{-i\\phi}\\sin\\theta/2 \\\\-i e^{i\\phi} \\sin\\theta/2 & \\cos\\theta/2\\end{array}\\right]$
$\\hat{n} = (\\cos\\phi, \\sin\\phi, 0)$ |RXY Q1 $\\phi~~\\theta$ |RZ Q1 $\\pi/2 − \\phi$
X2P Q1
RZ Q1 $\\theta$
X2M Q1
RZ Q1 $\\phi-\\pi/2$|\n", - "|RST | 主动复位操作 | RST Q1 | M Q1
CONDX Q1|\n", "\n", "注:\n", "\n", -- Gitee