Source code for qolumbina.programs.state_preparation.w_state
# The original code is sourced from:
# https://github.com/munich-quantum-toolkit/bench/blob/main/src/mqt/bench/benchmarks/wstate.py
#
# This program is adapted for use as a benchmark in controlled software testing experiments.
# Modifications made to the original code include (for Apache License 2.0):
# - Modify the code in the class
# - Remove the measurements from the circuit
#
# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
# All rights reserved.
#
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License
"""W state benchmark definition."""
from __future__ import annotations
import numpy as np
from qiskit.circuit import QuantumCircuit, QuantumRegister
# ---------- benchmark registration ----------
from ..benchmark_registry import register_benchmark
from pathlib import Path
@register_benchmark(
Path(__file__).stem,
family=Path(__file__).resolve().parent.name,
description="W state preparation circuit",
class_name="WState",
source={
"repo": "https://github.com/munich-quantum-toolkit/bench/tree/main",
"file": "src/mqt/bench/benchmarks/wstate.py",
"language": "Qiskit",
"available_doc": False
},
testability_refactoring=[
"Structure reorganization"
]
)
def create_w_state(num_qubits: int):
return WState(num_qubits=num_qubits)
[docs]
class WState(QuantumCircuit):
"""
Create a W State:
A W state is an entangled quantum state of multiple qubits where a single excitation
is delocalized over all qubits.
"""
def __init__(self, num_qubits: int, name: str | None = None) -> None:
"""
Args:
num_qubits: Number of qubits of the W state
name: Name of the quantum circuit
"""
self._num_qubits = num_qubits
super().__init__(num_qubits, name="wstate" or name)
# Build the W state circuit
self._build()
[docs]
def f_gate(self, qc: QuantumCircuit, i: int, j: int, n: int, k: int) -> None:
theta = np.arccos(np.sqrt(1 / (n - k + 1)))
qc.ry(-theta, j)
qc.cz(i, j)
qc.ry(theta, j)
def _build(self) -> None:
"""Build the W state circuit."""
num_qubits = self._num_qubits
q = QuantumRegister(num_qubits, "q")
qc = QuantumCircuit(q, name="wstate")
qc.x(q[-1])
for m in range(1, num_qubits):
self.f_gate(qc, num_qubits - m, num_qubits - m - 1, num_qubits, m)
for k in reversed(range(1, num_qubits)):
qc.cx(k - 1, k)
self.compose(qc, qubits=self.qubits, inplace=True)