Skip to content

Commit

Permalink
update readme and add gate test (#14)
Browse files Browse the repository at this point in the history
* update readme and add gate test

* set minimum numpy version

* update status to stable

* re-add pytest requirement
  • Loading branch information
splch authored Aug 13, 2023
1 parent 507850c commit 7bf6564
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Apply various quantum gates to the qubits:

```python
simulator.h(0) # Hadamard gate
simulator.x(1) # Pauli-X gate
simulator.cx(0, 1) # Controlled-Not gate
simulator.t(1) # π/8 gate
simulator.cx(0, 2) # Controlled-Not gate
```

### Custom Gates
Expand All @@ -40,7 +40,7 @@ Define and apply custom gates using angles:
import numpy as np

theta, phi, lambda_ = np.pi/4, np.pi/3, np.pi/2
simulator.u(0, theta, phi, lambda_) # Generic gate
simulator.u(1, theta, phi, lambda_) # Generic gate
```

### Measurements
Expand All @@ -51,7 +51,7 @@ Measure the state of the qubits:
results = simulator.run(shots=100)
```

> {'100': 24, '110': 30, '000': 29, '010': 17}
> {'010': 24, '101': 28, '111': 25, '000': 23}
### Circuit Representation

Expand All @@ -63,15 +63,19 @@ print(simulator)

```plaintext
-----------------
| H | | C | U |
| | X | X | |
| | | | |
| H | | C | |
| | T | | U |
| | | X | |
-----------------
```

## Testing

Tests are included in the package to verify its functionality.
Tests are included in the package to verify its functionality:

```shell
python3 -m pytest tests/
```

## License

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
numpy
pytest
numpy>=1.16.0
pytest>=2.1.0
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

setup(
name="qubit_simulator",
version="0.0.1",
version="0.0.2",
description="A simple qubit simulator",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
author="Spencer Churchill",
author_email="churchill@ionq.com",
packages=find_packages(),
install_requires=["numpy"],
install_requires=["numpy>=1.10.1"],
classifiers=[
"Development Status :: 3 - Alpha",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import numpy as np
from qubit_simulator import Gates


def test_create_controlled_gate():
# Test for a controlled-X gate
controlled_X = Gates.create_controlled_gate(Gates.X, 0, 1, 2)
expected_result = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
assert np.allclose(controlled_X, expected_result)
11 changes: 7 additions & 4 deletions tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,21 @@ def test_gate_reversibility():


def test_measure_probabilities():
shots = 10000
shots = 1000
simulator = QubitSimulator(1)
simulator.h(0)
results = simulator.run(shots=shots)
assert abs(results.get("0", 0) - results.get("1", 0)) < shots / 4


def test_str():
def test_circuit_string():
simulator = QubitSimulator(3)
simulator.h(0)
simulator.cx(0, 2)
expected_string = "---------\n| H | C |\n| | |\n| | X |\n---------"
simulator.x(1)
simulator.cx(2, 1)
expected_string = (
"-------------\n| H | | |\n| | X | X |\n| | | C |\n-------------"
)
assert str(simulator) == expected_string


Expand Down

0 comments on commit 7bf6564

Please sign in to comment.