quantum-simulator 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ """
2
+ Quantum Computer Simulator
3
+
4
+ A Python library for simulating quantum computers and quantum algorithms.
5
+ """
6
+
7
+ __version__ = "0.1.2"
8
+ __author__ = "Nathaniel Schultz"
9
+ __email__ = "nate.schultz@outlook.com"
10
+
11
+ from .simulator import QuantumSimulator
12
+ from .gates import QuantumGate
13
+ from .circuits import QuantumCircuit
14
+
15
+ __all__ = ["QuantumSimulator", "QuantumGate", "QuantumCircuit"]
@@ -0,0 +1,48 @@
1
+ """
2
+ Quantum circuits implementation.
3
+ """
4
+
5
+ from typing import List, Tuple
6
+ from .simulator import QuantumSimulator
7
+ from .gates import QuantumGate
8
+
9
+
10
+ class QuantumCircuit:
11
+ """A quantum circuit representation."""
12
+
13
+ def __init__(self, num_qubits: int):
14
+ """
15
+ Initialize a quantum circuit.
16
+
17
+ Args:
18
+ num_qubits: Number of qubits in the circuit
19
+ """
20
+ self.num_qubits = num_qubits
21
+ self.gates: List[Tuple[QuantumGate, List[int]]] = []
22
+
23
+ def add_gate(self, gate: QuantumGate, target_qubits: List[int]) -> None:
24
+ """
25
+ Add a gate to the circuit.
26
+
27
+ Args:
28
+ gate: The quantum gate to add
29
+ target_qubits: List of qubit indices the gate acts on
30
+ """
31
+ self.gates.append((gate, target_qubits))
32
+
33
+ def execute(self, simulator: QuantumSimulator) -> None:
34
+ """
35
+ Execute the circuit on a quantum simulator.
36
+
37
+ Args:
38
+ simulator: The quantum simulator to run the circuit on
39
+ """
40
+ for gate, target_qubits in self.gates:
41
+ simulator.state_vector = gate.apply(simulator.state_vector, target_qubits)
42
+
43
+ def __str__(self) -> str:
44
+ """String representation of the circuit."""
45
+ circuit_str = f"QuantumCircuit({self.num_qubits} qubits)\n"
46
+ for i, (gate, targets) in enumerate(self.gates):
47
+ circuit_str += f" {i}: {gate.name} on qubits {targets}\n"
48
+ return circuit_str
@@ -0,0 +1,96 @@
1
+ """
2
+ Quantum gates implementation.
3
+ """
4
+
5
+ import numpy as np
6
+ from typing import List
7
+
8
+
9
+ class QuantumGate:
10
+ """Base class for quantum gates."""
11
+
12
+ def __init__(self, name: str, matrix: np.ndarray):
13
+ """
14
+ Initialize a quantum gate.
15
+
16
+ Args:
17
+ name: Name of the gate
18
+ matrix: Unitary matrix representing the gate
19
+ """
20
+ self.name = name
21
+ self.matrix = matrix
22
+ self.num_qubits = int(np.log2(matrix.shape[0]))
23
+
24
+ def apply(self, state_vector: np.ndarray, target_qubits: List[int]) -> np.ndarray:
25
+ """
26
+ Apply the gate to specific qubits.
27
+
28
+ Args:
29
+ state_vector: Current quantum state vector
30
+ target_qubits: List of qubit indices to apply the gate to
31
+
32
+ Returns:
33
+ New state vector after applying the gate
34
+ """
35
+ n_qubits = int(np.log2(len(state_vector)))
36
+
37
+ if self.num_qubits == 1 and len(target_qubits) == 1:
38
+ # Single-qubit gate
39
+ return self._apply_single_qubit_gate(state_vector, target_qubits[0], n_qubits)
40
+ elif self.num_qubits == 2 and len(target_qubits) == 2:
41
+ # Two-qubit gate (like CNOT)
42
+ return self._apply_two_qubit_gate(state_vector, target_qubits[0], target_qubits[1], n_qubits)
43
+ else:
44
+ raise ValueError(f"Gate requires {self.num_qubits} qubits, got {len(target_qubits)}")
45
+
46
+ def _apply_single_qubit_gate(self, state_vector: np.ndarray, target_qubit: int, n_qubits: int) -> np.ndarray:
47
+ """Apply a single-qubit gate to the state vector."""
48
+ new_state = np.zeros_like(state_vector)
49
+
50
+ for i in range(len(state_vector)):
51
+ # Extract the bit value of the target qubit from state index i
52
+ bit_val = (i >> target_qubit) & 1
53
+
54
+ # Apply gate matrix
55
+ for new_bit_val in range(2):
56
+ # Flip the target qubit bit to new_bit_val
57
+ new_i = i ^ (bit_val << target_qubit) | (new_bit_val << target_qubit)
58
+ new_state[new_i] += self.matrix[new_bit_val, bit_val] * state_vector[i]
59
+
60
+ return new_state
61
+
62
+ def _apply_two_qubit_gate(self, state_vector: np.ndarray, control_qubit: int, target_qubit: int, n_qubits: int) -> np.ndarray:
63
+ """Apply a two-qubit gate to the state vector."""
64
+ new_state = np.zeros_like(state_vector)
65
+
66
+ for i in range(len(state_vector)):
67
+ # Extract control and target qubit values
68
+ control_bit = (i >> control_qubit) & 1
69
+ target_bit = (i >> target_qubit) & 1
70
+
71
+ # For CNOT: flip target if control is 1, otherwise leave unchanged
72
+ if self.name == "CNOT":
73
+ if control_bit == 1:
74
+ # Flip the target qubit
75
+ flipped_i = i ^ (1 << target_qubit)
76
+ new_state[flipped_i] += state_vector[i]
77
+ else:
78
+ # Control is 0, no change
79
+ new_state[i] += state_vector[i]
80
+
81
+ return new_state
82
+
83
+
84
+ # Common single-qubit gates
85
+ X_GATE = QuantumGate("X", np.array([[0, 1], [1, 0]], dtype=complex))
86
+ Y_GATE = QuantumGate("Y", np.array([[0, -1j], [1j, 0]], dtype=complex))
87
+ Z_GATE = QuantumGate("Z", np.array([[1, 0], [0, -1]], dtype=complex))
88
+ H_GATE = QuantumGate("H", np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2))
89
+
90
+ # Two-qubit gates
91
+ CNOT_GATE = QuantumGate("CNOT", np.array([
92
+ [1, 0, 0, 0],
93
+ [0, 1, 0, 0],
94
+ [0, 0, 0, 1],
95
+ [0, 0, 1, 0]
96
+ ], dtype=complex))
@@ -0,0 +1,64 @@
1
+ """
2
+ Quantum simulator implementation.
3
+ """
4
+
5
+ import numpy as np
6
+ from typing import List, Tuple, Optional
7
+
8
+
9
+ class QuantumSimulator:
10
+ """A quantum computer simulator."""
11
+
12
+ def __init__(self, num_qubits: int):
13
+ """
14
+ Initialize the quantum simulator.
15
+
16
+ Args:
17
+ num_qubits: Number of qubits in the quantum system
18
+ """
19
+ self.num_qubits = num_qubits
20
+ self.num_states = 2 ** num_qubits
21
+ # Initialize all qubits in |0> state
22
+ self.state_vector = np.zeros(self.num_states, dtype=complex)
23
+ self.state_vector[0] = 1.0
24
+
25
+ def reset(self) -> None:
26
+ """Reset all qubits to |0> state."""
27
+ self.state_vector = np.zeros(self.num_states, dtype=complex)
28
+ self.state_vector[0] = 1.0
29
+
30
+ def get_state_vector(self) -> np.ndarray:
31
+ """Get the current state vector."""
32
+ return self.state_vector.copy()
33
+
34
+ def measure(self, qubit: int) -> int:
35
+ """
36
+ Measure a specific qubit.
37
+
38
+ Args:
39
+ qubit: Index of the qubit to measure (0-indexed)
40
+
41
+ Returns:
42
+ Measurement result (0 or 1)
43
+ """
44
+ # This is a simplified measurement implementation
45
+ probabilities = np.abs(self.state_vector) ** 2
46
+ # Calculate probability of measuring |0> for the specified qubit
47
+ prob_0 = sum(probabilities[i] for i in range(self.num_states)
48
+ if not (i >> qubit) & 1)
49
+
50
+ # Simulate measurement outcome
51
+ result = 0 if np.random.random() < prob_0 else 1
52
+
53
+ # Collapse the state vector (simplified)
54
+ new_state = np.zeros_like(self.state_vector)
55
+ norm = 0
56
+ for i in range(self.num_states):
57
+ if ((i >> qubit) & 1) == result:
58
+ new_state[i] = self.state_vector[i]
59
+ norm += abs(self.state_vector[i]) ** 2
60
+
61
+ if norm > 0:
62
+ self.state_vector = new_state / np.sqrt(norm)
63
+
64
+ return result
@@ -0,0 +1,218 @@
1
+ Metadata-Version: 2.4
2
+ Name: quantum-simulator
3
+ Version: 0.1.2
4
+ Summary: A Python library for simulating quantum computers and quantum algorithms
5
+ Project-URL: Homepage, https://github.com/beefy/quantum-simulator
6
+ Project-URL: Documentation, https://beefy.github.io/quantum-simulator/
7
+ Project-URL: Repository, https://github.com/beefy/quantum-simulator.git
8
+ Project-URL: Issues, https://github.com/beefy/quantum-simulator/issues
9
+ Project-URL: Changelog, https://github.com/beefy/quantum-simulator/blob/main/CHANGELOG.md
10
+ Author-email: Nathaniel Schultz <nate.schultz@outlook.com>
11
+ Maintainer-email: Nathaniel Schultz <nate.schultz@outlook.com>
12
+ License: Unlicense
13
+ License-File: LICENSE
14
+ Keywords: computing,quantum,quantum-algorithms,simulation
15
+ Classifier: Development Status :: 3 - Alpha
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Intended Audience :: Science/Research
18
+ Classifier: License :: Public Domain
19
+ Classifier: Operating System :: OS Independent
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.8
22
+ Classifier: Programming Language :: Python :: 3.9
23
+ Classifier: Programming Language :: Python :: 3.10
24
+ Classifier: Programming Language :: Python :: 3.11
25
+ Classifier: Programming Language :: Python :: 3.12
26
+ Classifier: Topic :: Scientific/Engineering :: Physics
27
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
28
+ Requires-Python: >=3.8
29
+ Requires-Dist: numpy>=1.20.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: black; extra == 'dev'
32
+ Requires-Dist: flake8; extra == 'dev'
33
+ Requires-Dist: mypy; extra == 'dev'
34
+ Requires-Dist: pre-commit; extra == 'dev'
35
+ Requires-Dist: pytest-cov; extra == 'dev'
36
+ Requires-Dist: pytest>=6.0; extra == 'dev'
37
+ Provides-Extra: docs
38
+ Requires-Dist: mkdocs-autorefs; extra == 'docs'
39
+ Requires-Dist: mkdocs-gen-files; extra == 'docs'
40
+ Requires-Dist: mkdocs-literate-nav; extra == 'docs'
41
+ Requires-Dist: mkdocs-material>=9.4.0; extra == 'docs'
42
+ Requires-Dist: mkdocs-section-index; extra == 'docs'
43
+ Requires-Dist: mkdocs>=1.4.0; extra == 'docs'
44
+ Requires-Dist: mkdocstrings[python]>=0.19.0; extra == 'docs'
45
+ Requires-Dist: pymdown-extensions>=9.0.0; extra == 'docs'
46
+ Provides-Extra: test
47
+ Requires-Dist: pytest-cov; extra == 'test'
48
+ Requires-Dist: pytest>=6.0; extra == 'test'
49
+ Description-Content-Type: text/markdown
50
+
51
+ # Quantum Simulator
52
+
53
+ [![Tests](https://github.com/beefy/quantum-simulator/actions/workflows/tests.yml/badge.svg)](https://github.com/beefy/quantum-simulator/actions/workflows/tests.yml)
54
+ [![PyPI version](https://badge.fury.io/py/quantum-simulator.svg)](https://badge.fury.io/py/quantum-simulator)
55
+ [![Documentation](https://img.shields.io/badge/docs-mkdocs-blue)](https://beefy.github.io/quantum-simulator/)
56
+ [![Python versions](https://img.shields.io/pypi/pyversions/quantum-simulator)](https://pypi.org/project/quantum-simulator/)
57
+ [![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/)
58
+
59
+ A Python library for simulating quantum computers and quantum algorithms. This package provides an easy-to-use interface for quantum state simulation, gate operations, and circuit execution.
60
+
61
+ ## Features
62
+
63
+ - 🔬 **Quantum State Simulation**: Accurate simulation of quantum states using state vectors
64
+ - 🚪 **Quantum Gates**: Implementation of common single and multi-qubit gates (X, Y, Z, H, CNOT)
65
+ - 🔗 **Quantum Circuits**: Build and execute complex quantum circuits
66
+ - 📊 **Measurement**: Simulate quantum measurements with proper state collapse
67
+ - 🎯 **Easy to Use**: Clean, intuitive API for quantum programming
68
+ - 📚 **Well Documented**: Comprehensive documentation and examples
69
+
70
+ ## Quick Start
71
+
72
+ ### Installation
73
+
74
+ ```bash
75
+ pip install quantum-simulator
76
+ ```
77
+
78
+ ### Basic Example
79
+
80
+ ```python
81
+ from quantum_simulator import QuantumSimulator, QuantumCircuit
82
+ from quantum_simulator.gates import H_GATE, CNOT_GATE
83
+
84
+ # Create a 2-qubit quantum simulator
85
+ sim = QuantumSimulator(2)
86
+
87
+ # Build a Bell state circuit
88
+ circuit = QuantumCircuit(2)
89
+ circuit.add_gate(H_GATE, [0]) # Hadamard on qubit 0
90
+ circuit.add_gate(CNOT_GATE, [0, 1]) # CNOT with control=0, target=1
91
+
92
+ # Execute the circuit
93
+ circuit.execute(sim)
94
+
95
+ # Measure the qubits
96
+ result0 = sim.measure(0)
97
+ result1 = sim.measure(1)
98
+ print(f"Measurement: {result0}, {result1}")
99
+ ```
100
+
101
+ ## Documentation
102
+
103
+ Full documentation is available at **[beefy.github.io/quantum-simulator](https://beefy.github.io/quantum-simulator/)**
104
+
105
+ - [Installation Guide](https://beefy.github.io/quantum-simulator/getting-started/installation/)
106
+ - [Quick Start](https://beefy.github.io/quantum-simulator/getting-started/quickstart/)
107
+ - [API Reference](https://beefy.github.io/quantum-simulator/reference/)
108
+ - [Examples](https://beefy.github.io/quantum-simulator/getting-started/examples/)
109
+
110
+ ## Development
111
+
112
+ ### Setting Up Development Environment
113
+
114
+ 1. **Clone the repository**:
115
+ ```bash
116
+ git clone https://github.com/beefy/quantum-simulator.git
117
+ cd quantum-simulator
118
+ ```
119
+
120
+ 2. **Install in development mode**:
121
+ ```bash
122
+ pip install -e .[dev,docs]
123
+ ```
124
+
125
+ 3. **Run tests**:
126
+ ```bash
127
+ pytest --cov=quantum_simulator --cov-report=xml --cov-report=term
128
+ ```
129
+
130
+ 4. **Run lint checks**:
131
+ ```bash
132
+ flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
133
+ mypy src/
134
+ ```
135
+
136
+ 4. **Build documentation**:
137
+ ```bash
138
+ mkdocs serve
139
+ ```
140
+
141
+ ## Publishing
142
+
143
+ ### Publishing to PyPI
144
+
145
+ This project uses automated publishing via GitHub Actions. To publish a new version:
146
+
147
+ #### 1. Prepare the Release
148
+
149
+ 1. **Update version** in `src/quantum_simulator/__init__.py`:
150
+ ```python
151
+ __version__ = "0.2.0" # Increment version number
152
+ ```
153
+
154
+ 2. **Update CHANGELOG.md** with release notes
155
+
156
+ 3. **Commit changes**:
157
+ ```bash
158
+ git add .
159
+ git commit -m "Bump version to 0.2.0"
160
+ git push
161
+ ```
162
+
163
+ #### 2. Create a Release
164
+
165
+ 1. **Create and push a tag**:
166
+ ```bash
167
+ git tag v0.2.0
168
+ git push origin v0.2.0
169
+ ```
170
+
171
+ 2. **Create a GitHub Release**:
172
+ - Go to [GitHub Releases](https://github.com/beefy/quantum-simulator/releases)
173
+ - Click "Create a new release"
174
+ - Choose the tag you just created
175
+ - Add release notes
176
+ - Click "Publish release"
177
+
178
+ 3. **Automated Publishing**:
179
+ - GitHub Actions will automatically build and publish to PyPI
180
+ - Check the [Actions tab](https://github.com/beefy/quantum-simulator/actions) for progress
181
+
182
+ #### 3. Manual Publishing (if needed)
183
+
184
+ If automated publishing fails, you can publish manually:
185
+
186
+ ```bash
187
+ # Install build tools
188
+ pip install build twine
189
+
190
+ # Build the package
191
+ python -m build
192
+
193
+ # Upload to PyPI (requires PyPI API token)
194
+ twine upload dist/*
195
+ ```
196
+
197
+ ### Publishing Documentation
198
+
199
+ Documentation is automatically deployed to GitHub Pages on every push to the `main` branch.
200
+
201
+ #### Manual Documentation Deployment
202
+
203
+ ```bash
204
+ # Install documentation dependencies
205
+ pip install -e .[docs]
206
+
207
+ # Build and deploy to GitHub Pages
208
+ mkdocs gh-deploy
209
+ ```
210
+
211
+ #### Local Documentation Development
212
+
213
+ ```bash
214
+ # Serve documentation locally with auto-reload
215
+ mkdocs serve
216
+
217
+ # Open http://localhost:8000 in your browser
218
+ ```
@@ -0,0 +1,9 @@
1
+ quantum_simulator/__init__.py,sha256=FoEw_1BFNSoVnGICjGQKbeeDiMjx1zWmA2oo8TF-h-c,378
2
+ quantum_simulator/circuits.py,sha256=GEuH9qfOH0yYiC-T_3hADjpo55rwX84JT4oMtDrEVWE,1492
3
+ quantum_simulator/gates.py,sha256=TP3WE09fyd6B-YSpJ0cOV5PFQ0FPuZY3Cc0HJUXNjLA,3599
4
+ quantum_simulator/simulator.py,sha256=kI_UxbQZr9NG_ZfGR3oZk9D2DM4qHLp7S6OYSsig-Cc,2018
5
+ quantum_simulator-0.1.2.dist-info/METADATA,sha256=HztRRcoHD-yk8OsS4AAUMAAPKmtRwOjRgZV1knd2Bzc,6986
6
+ quantum_simulator-0.1.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
+ quantum_simulator-0.1.2.dist-info/entry_points.txt,sha256=ASQZ3hc87SvvB-KxgD3ZAxh09k4HWzJX0iciD1D5st4,59
8
+ quantum_simulator-0.1.2.dist-info/licenses/LICENSE,sha256=awOCsWJ58m_2kBQwBUGWejVqZm6wuRtCL2hi9rfa0X4,1211
9
+ quantum_simulator-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ quantum-sim = quantum_simulator.cli:main
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>