# QKDpy: Quantum Key Distribution Library
[](https://opensource.org/licenses/Apache-2.0)
[](https://www.python.org/downloads/)
[](https://github.com/yourusername/qkdpy/actions/workflows/ci.yml)
[](https://github.com/yourusername/qkdpy/actions/workflows/release.yml)
[](https://qkdpy.readthedocs.io/)
QKDpy is a comprehensive Python library for Quantum Key Distribution (QKD) simulations, implementing various QKD protocols, quantum simulators, and cryptographic tools. It provides an intuitive API similar to NumPy, TensorFlow, and scikit-learn, making quantum cryptography accessible to developers and researchers.
## Features
- **Quantum Simulation**: Simulate qubits, quantum gates (now with individual gate classes for better modularity), multi-qubit states, and measurements (with flexible state collapse for research and visualization)
- **QKD Protocols**: Implementations of BB84, E92, E91, SARG04, CV-QKD, Device-Independent QKD, HD-QKD, and more
- **High-Dimensional QKD**: Support for qudit-based protocols with enhanced security and key rates
- **Key Management**: Advanced error correction and privacy amplification algorithms
- **Quantum Cryptography**: Quantum authentication, key exchange, and random number generation
- **Enhanced Security**: Message authentication, key validation, and side-channel protection
- **Machine Learning Integration**: Optimization and anomaly detection for QKD systems
- **Quantum Networks**: Multi-party QKD and network simulation capabilities
- **Visualization**: Advanced tools to visualize quantum states and protocol execution
- **Quantum Network Analysis**: Tools for analyzing quantum networks and multi-party QKD
- **Extensible Design**: Easy to add new protocols and features
- **Performance**: Efficient implementations for simulations
## Installation
QKDpy requires Python 3.10 or higher. We recommend using [uv](https://github.com/astral-sh/uv) for package management:
```bash
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone the repository
git clone https://github.com/yourusername/qkdpy.git
cd qkdpy
# Create a virtual environment
uv venv
# Install in development mode
uv pip install -e .
```
Or using pip with a virtual environment:
```bash
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install in development mode
pip install -e .
```
## Quick Start
Here's a simple example of using the BB84 protocol to generate a secure key:
```python
from qkdpy import BB84, QuantumChannel, Qubit
from qkdpy.core import PauliX, Hadamard # Import individual gate classes
# Create a quantum channel with some noise
channel = QuantumChannel(loss=0.1, noise_model='depolarizing', noise_level=0.05)
# Create a BB84 protocol instance
bb84 = BB84(channel, key_length=100)
# Execute the protocol
results = bb84.execute()
# Print the results
print(f"Generated key: {results['final_key']}")
print(f"QBER: {results['qber']:.4f}")
print(f"Is secure: {results['is_secure']}")
# Example of flexible qubit measurement and collapse
q = Qubit.plus() # Qubit in superposition
print(f"Qubit state before measurement: {q.state}")
measurement_result = q.measure("hadamard") # Measure without collapsing internal state
print(f"Measurement result: {measurement_result}")
print(f"Qubit state after measurement (still in superposition): {q.state}")
q.collapse_state(measurement_result, "hadamard") # Explicitly collapse the state
print(f"Qubit state after explicit collapse: {q.state}")
# Example of applying a gate
q_zero = Qubit.zero()
print(f"Qubit state before X gate: {q_zero.state}")
q_zero.apply_gate(PauliX().matrix) # Apply Pauli-X gate
print(f"Qubit state after X gate: {q_zero.state}")
```
For High-Dimensional QKD:
```python
from qkdpy import HDQKD, QuantumChannel
# Create a quantum channel with some noise
channel = QuantumChannel(loss=0.1, noise_model='depolarizing', noise_level=0.05)
# Create an HD-QKD protocol instance with 4-dimensional qudits
hd_qkd = HDQKD(channel, key_length=100, dimension=4)
# Execute the protocol
results = hd_qkd.execute()
# Print the results
print(f"Generated key: {results['final_key']}")
print(f"QBER: {results['qber']:.4f}")
print(f"Is secure: {results['is_secure']}")
print(f"Dimensional efficiency gain: {hd_qkd.get_dimension_efficiency():.2f}x")
```
For more examples, see the examples directory.
## Advanced Usage
QKDpy also supports advanced protocols and features:
```python
from qkdpy import (
DeviceIndependentQKD,
QuantumKeyManager,
QuantumRandomNumberGenerator,
QuantumNetwork,
HDQKD,
QKDOptimizer
)
# Device-independent QKD
di_qkd = DeviceIndependentQKD(channel, key_length=100)
results = di_qkd.execute()
# Quantum key management
key_manager = QuantumKeyManager(channel)
key_id = key_manager.generate_key("secure_session", key_length=128)
# Quantum random number generation
qrng = QuantumRandomNumberGenerator(channel)
random_bits = qrng.generate_random_bits(100)
# Quantum network simulation
network = QuantumNetwork("Research Network")
network.add_node("Alice")
network.add_node("Bob")
network.add_connection("Alice", "Bob", channel)
key = network.establish_key_between_nodes("Alice", "Bob", 128)
# High-dimensional QKD
hd_qkd = HDQKD(channel, key_length=100, dimension=4)
hd_results = hd_qkd.execute()
# ML-based QKD optimization
optimizer = QKDOptimizer("BB84")
parameter_space = {
"loss": (0.0, 0.5),
"noise_level": (0.0, 0.1)
}
# optimization_results = optimizer.optimize_channel_parameters(
# parameter_space,
# lambda params: simulate_protocol_performance(params),
# num_iterations=50
# )
```
## Contributing
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
## License
QKDpy is licensed under the Apache License 2.0. See [LICENSE](LICENSE) for the full license text.
## Citation
If you use QKDpy in your research, please cite it as described in [CITATION.cff](CITATION.cff).
## Code of Conduct
This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.
## Contact
For questions, suggestions, or issues, please open an [issue](https://github.com/yourusername/qkdpy/issues) on GitHub.
Raw data
{
"_id": null,
"home_page": null,
"name": "qkdpy",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "Pranava-Kumar <pranavakumar.it@gmail.com>",
"keywords": "quantum, cryptography, qkd, simulation, hd-qkd, quantum-networks",
"author": null,
"author_email": "Pranava-Kumar <pranavakumar.it@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/d0/1d/64b5ae265a7d30e4d15ce9b718e2c645bff60ce433338e56e413ea76b95e/qkdpy-0.2.9.tar.gz",
"platform": null,
"description": "# QKDpy: Quantum Key Distribution Library\n\n[](https://opensource.org/licenses/Apache-2.0)\n[](https://www.python.org/downloads/)\n[](https://github.com/yourusername/qkdpy/actions/workflows/ci.yml)\n[](https://github.com/yourusername/qkdpy/actions/workflows/release.yml)\n[](https://qkdpy.readthedocs.io/)\n\nQKDpy is a comprehensive Python library for Quantum Key Distribution (QKD) simulations, implementing various QKD protocols, quantum simulators, and cryptographic tools. It provides an intuitive API similar to NumPy, TensorFlow, and scikit-learn, making quantum cryptography accessible to developers and researchers.\n\n## Features\n\n- **Quantum Simulation**: Simulate qubits, quantum gates (now with individual gate classes for better modularity), multi-qubit states, and measurements (with flexible state collapse for research and visualization)\n- **QKD Protocols**: Implementations of BB84, E92, E91, SARG04, CV-QKD, Device-Independent QKD, HD-QKD, and more\n- **High-Dimensional QKD**: Support for qudit-based protocols with enhanced security and key rates\n- **Key Management**: Advanced error correction and privacy amplification algorithms\n- **Quantum Cryptography**: Quantum authentication, key exchange, and random number generation\n- **Enhanced Security**: Message authentication, key validation, and side-channel protection\n- **Machine Learning Integration**: Optimization and anomaly detection for QKD systems\n- **Quantum Networks**: Multi-party QKD and network simulation capabilities\n- **Visualization**: Advanced tools to visualize quantum states and protocol execution\n- **Quantum Network Analysis**: Tools for analyzing quantum networks and multi-party QKD\n- **Extensible Design**: Easy to add new protocols and features\n- **Performance**: Efficient implementations for simulations\n\n## Installation\n\nQKDpy requires Python 3.10 or higher. We recommend using [uv](https://github.com/astral-sh/uv) for package management:\n\n```bash\n# Install uv\ncurl -LsSf https://astral.sh/uv/install.sh | sh\n\n# Clone the repository\ngit clone https://github.com/yourusername/qkdpy.git\ncd qkdpy\n\n# Create a virtual environment\nuv venv\n\n# Install in development mode\nuv pip install -e .\n```\n\nOr using pip with a virtual environment:\n\n```bash\n# Create a virtual environment\npython -m venv .venv\nsource .venv/bin/activate # On Windows: .venv\\Scripts\\activate\n\n# Install in development mode\npip install -e .\n```\n\n## Quick Start\n\nHere's a simple example of using the BB84 protocol to generate a secure key:\n\n```python\nfrom qkdpy import BB84, QuantumChannel, Qubit\nfrom qkdpy.core import PauliX, Hadamard # Import individual gate classes\n\n# Create a quantum channel with some noise\nchannel = QuantumChannel(loss=0.1, noise_model='depolarizing', noise_level=0.05)\n\n# Create a BB84 protocol instance\nbb84 = BB84(channel, key_length=100)\n\n# Execute the protocol\nresults = bb84.execute()\n\n# Print the results\nprint(f\"Generated key: {results['final_key']}\")\nprint(f\"QBER: {results['qber']:.4f}\")\nprint(f\"Is secure: {results['is_secure']}\")\n\n# Example of flexible qubit measurement and collapse\nq = Qubit.plus() # Qubit in superposition\nprint(f\"Qubit state before measurement: {q.state}\")\nmeasurement_result = q.measure(\"hadamard\") # Measure without collapsing internal state\nprint(f\"Measurement result: {measurement_result}\")\nprint(f\"Qubit state after measurement (still in superposition): {q.state}\")\nq.collapse_state(measurement_result, \"hadamard\") # Explicitly collapse the state\nprint(f\"Qubit state after explicit collapse: {q.state}\")\n\n# Example of applying a gate\nq_zero = Qubit.zero()\nprint(f\"Qubit state before X gate: {q_zero.state}\")\nq_zero.apply_gate(PauliX().matrix) # Apply Pauli-X gate\nprint(f\"Qubit state after X gate: {q_zero.state}\")\n```\n\nFor High-Dimensional QKD:\n\n```python\nfrom qkdpy import HDQKD, QuantumChannel\n\n# Create a quantum channel with some noise\nchannel = QuantumChannel(loss=0.1, noise_model='depolarizing', noise_level=0.05)\n\n# Create an HD-QKD protocol instance with 4-dimensional qudits\nhd_qkd = HDQKD(channel, key_length=100, dimension=4)\n\n# Execute the protocol\nresults = hd_qkd.execute()\n\n# Print the results\nprint(f\"Generated key: {results['final_key']}\")\nprint(f\"QBER: {results['qber']:.4f}\")\nprint(f\"Is secure: {results['is_secure']}\")\nprint(f\"Dimensional efficiency gain: {hd_qkd.get_dimension_efficiency():.2f}x\")\n```\n\nFor more examples, see the examples directory.\n\n## Advanced Usage\n\nQKDpy also supports advanced protocols and features:\n\n```python\nfrom qkdpy import (\n DeviceIndependentQKD,\n QuantumKeyManager,\n QuantumRandomNumberGenerator,\n QuantumNetwork,\n HDQKD,\n QKDOptimizer\n)\n\n# Device-independent QKD\ndi_qkd = DeviceIndependentQKD(channel, key_length=100)\nresults = di_qkd.execute()\n\n# Quantum key management\nkey_manager = QuantumKeyManager(channel)\nkey_id = key_manager.generate_key(\"secure_session\", key_length=128)\n\n# Quantum random number generation\nqrng = QuantumRandomNumberGenerator(channel)\nrandom_bits = qrng.generate_random_bits(100)\n\n# Quantum network simulation\nnetwork = QuantumNetwork(\"Research Network\")\nnetwork.add_node(\"Alice\")\nnetwork.add_node(\"Bob\")\nnetwork.add_connection(\"Alice\", \"Bob\", channel)\nkey = network.establish_key_between_nodes(\"Alice\", \"Bob\", 128)\n\n# High-dimensional QKD\nhd_qkd = HDQKD(channel, key_length=100, dimension=4)\nhd_results = hd_qkd.execute()\n\n# ML-based QKD optimization\noptimizer = QKDOptimizer(\"BB84\")\nparameter_space = {\n \"loss\": (0.0, 0.5),\n \"noise_level\": (0.0, 0.1)\n}\n# optimization_results = optimizer.optimize_channel_parameters(\n# parameter_space,\n# lambda params: simulate_protocol_performance(params),\n# num_iterations=50\n# )\n```\n\n## Contributing\nWe welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.\n\n## License\nQKDpy is licensed under the Apache License 2.0. See [LICENSE](LICENSE) for the full license text.\n\n## Citation\nIf you use QKDpy in your research, please cite it as described in [CITATION.cff](CITATION.cff).\n\n## Code of Conduct\nThis project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.\n\n## Contact\nFor questions, suggestions, or issues, please open an [issue](https://github.com/yourusername/qkdpy/issues) on GitHub.\n",
"bugtrack_url": null,
"license": null,
"summary": "A Python Package for Quantum Key Distribution",
"version": "0.2.9",
"project_urls": {
"Bug Tracker": "https://github.com/Pranava-Kumar/qkdpy/issues",
"Homepage": "https://github.com/Pranava-Kumar/qkdpy",
"Repository": "https://github.com/Pranava-Kumar/qkdpy"
},
"split_keywords": [
"quantum",
" cryptography",
" qkd",
" simulation",
" hd-qkd",
" quantum-networks"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "23fea0c86a571b9e9664b1089a05c5b694914676cde118ea2a9f2ce139e267ed",
"md5": "6fc36786746174c3c66b7b768cef0625",
"sha256": "f4083dadbf65aa719aedb676db936557227788d882e1e55bd1bad51dcdfecb10"
},
"downloads": -1,
"filename": "qkdpy-0.2.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6fc36786746174c3c66b7b768cef0625",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 159295,
"upload_time": "2025-10-19T22:49:49",
"upload_time_iso_8601": "2025-10-19T22:49:49.542089Z",
"url": "https://files.pythonhosted.org/packages/23/fe/a0c86a571b9e9664b1089a05c5b694914676cde118ea2a9f2ce139e267ed/qkdpy-0.2.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d01d64b5ae265a7d30e4d15ce9b718e2c645bff60ce433338e56e413ea76b95e",
"md5": "099ea43afe278da7a906df33b1be363f",
"sha256": "5e09caf17f25f10c1532b83f1b3e57a08de91d8893b1e6bc8f3d5e5e3821c196"
},
"downloads": -1,
"filename": "qkdpy-0.2.9.tar.gz",
"has_sig": false,
"md5_digest": "099ea43afe278da7a906df33b1be363f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 124450,
"upload_time": "2025-10-19T22:49:51",
"upload_time_iso_8601": "2025-10-19T22:49:51.272580Z",
"url": "https://files.pythonhosted.org/packages/d0/1d/64b5ae265a7d30e4d15ce9b718e2c645bff60ce433338e56e413ea76b95e/qkdpy-0.2.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-19 22:49:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Pranava-Kumar",
"github_project": "qkdpy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "matplotlib",
"specs": [
[
">=",
"3.7.0"
]
]
},
{
"name": "numpy",
"specs": [
[
">=",
"1.24.0"
]
]
},
{
"name": "scipy",
"specs": [
[
">=",
"1.10.0"
]
]
}
],
"lcname": "qkdpy"
}