Source code for qolumbina.programs.state_preparation.ghz_state
# The original code is sourced from:
# https://github.com/munich-quantum-toolkit/bench/blob/main/src/mqt/bench/benchmarks/ghz.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
"""GHZ benchmark definition."""
from __future__ import annotations
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="GHZ state preparation circuit",
class_name="GHZState",
source={
"repo": "https://github.com/munich-quantum-toolkit/bench/tree/main",
"file": "src/mqt/bench/benchmarks/ghz.py",
"sdk": "Qiskit",
"available_doc": False
},
testability_refactoring=[
"Structure reorganization"
]
)
def create_ghz_state(num_qubits: int):
return GHZState(num_qubits=num_qubits)
[docs]
class GHZState(QuantumCircuit):
"""
Create a Greenberger-Horne-Zeilinger (GHZ) State:
A GHZ state is an entangled quantum state of multiple qubits where all qubits are
in a superposition of all being 0 or all being 1.
"""
def __init__(self, num_qubits: int, name: str | None = None) -> None:
r"""
Arguments:
num_qubits: Number of qubits of the GHZ state.
name: Name of the quantum circuit.
"""
self._num_qubits = num_qubits
super().__init__(num_qubits, name="ghz" or name)
# Build the GHZ state circuit
self._build()
def _build(self) -> None:
"""Build the GHZ state circuit."""
num_qubits = self._num_qubits
q = QuantumRegister(num_qubits, "q")
qc = QuantumCircuit(q, name="ghz")
qc.h(q[-1])
for i in range(1, num_qubits):
qc.cx(q[num_qubits - i], q[num_qubits - i - 1])
# qc.measure_all()
self.compose(qc, qubits=self.qubits, inplace=True)