{"id":27328696,"url":"https://github.com/10d9e/qsim","last_synced_at":"2025-04-12T12:16:37.036Z","repository":{"id":279501608,"uuid":"939020072","full_name":"10d9e/qsim","owner":"10d9e","description":"Quantum Circuit Simulator in Rust","archived":false,"fork":false,"pushed_at":"2025-02-25T21:53:20.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-25T22:29:49.391Z","etag":null,"topics":["quantum-computing","quantum-simulator"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/10d9e.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-25T21:33:07.000Z","updated_at":"2025-02-25T21:53:24.000Z","dependencies_parsed_at":"2025-02-25T22:41:21.521Z","dependency_job_id":null,"html_url":"https://github.com/10d9e/qsim","commit_stats":null,"previous_names":["10d9e/qsim"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10d9e%2Fqsim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10d9e%2Fqsim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10d9e%2Fqsim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10d9e%2Fqsim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/10d9e","download_url":"https://codeload.github.com/10d9e/qsim/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565093,"owners_count":21125418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["quantum-computing","quantum-simulator"],"created_at":"2025-04-12T12:16:32.852Z","updated_at":"2025-04-12T12:16:37.022Z","avatar_url":"https://github.com/10d9e.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Quantum Circuit Simulator in Rust\n\nA **Rust-based quantum circuit simulator** supporting multiple common gates (single-qubit, multi-qubit), measurement operations (including partial measurements of subsets of qubits), and arbitrary user-defined gates. This project demonstrates the foundational ideas of quantum computing using matrix-vector multiplication on a state vector representing \\(|\\psi\\rangle\\).\n\n\u003e **Table of Contents**  \n\u003e - [Features](#features)  \n\u003e - [Prerequisites](#prerequisites)  \n\u003e - [Project Structure](#project-structure)  \n\u003e - [Installation and Building](#installation-and-building)  \n\u003e - [Usage](#usage)  \n\u003e - [Complete Source Code](#complete-source-code)  \n\u003e   - [Cargo.toml](#cargotoml)  \n\u003e   - [src/main.rs](#srcmainrs)  \n\u003e - [Examples](#examples)  \n\u003e - [Contributing](#contributing)  \n\u003e - [License](#license)\n\n---\n\n## Features\n\n1. **N-Qubit State Representation**  \n   - Stores the wavefunction \\(|\\psi\\rangle\\) in a length-\\(2^N\\) complex vector.\n\n2. **Comprehensive Single-Qubit Gates**  \n   - \\(X, Y, Z\\), **Hadamard** (H), **Phase** gates (S, S\\(\\dagger\\), T, T\\(\\dagger\\), etc.), **sqrt(X)** (SX).\n\n3. **Multi-Qubit Gates**  \n   - **CNOT**, **CZ**, **SWAP**, **Toffoli** (CCNOT), and an **Arbitrary** multi-qubit gate with a custom \\(2^k \\times 2^k\\) matrix.\n\n4. **Measurement**  \n   - Single-qubit measurement (collapses the wavefunction to \\(|0\\rangle\\) or \\(|1\\rangle\\)).  \n   - Multi-qubit measurement on a subset of qubits.  \n   - Measurement of all qubits at once, collapsing the state to a single basis.\n\n5. **Circuit Model**  \n   - Construct a `QuantumCircuit` by adding gates in sequence.  \n   - Apply the circuit to a `QuantumState`, which automatically updates all amplitudes.\n\n6. **Extensibility**  \n   - Easily add new gates by providing a suitable matrix.  \n   - General multi-qubit gate application logic is included.\n\n---\n\n## Prerequisites\n\n- [Rust](https://www.rust-lang.org/) (1.56+ recommended)  \n- [Cargo](https://doc.rust-lang.org/cargo/) (typically installed alongside Rust)  \n- (Optional) [Git](https://git-scm.com/) for version control and pushing to GitHub  \n\n---\n\n## Project Structure\n\n```\nquantum-circuit-simulator/\n├── Cargo.toml\n├── README.md  \u003c-- (you are here)\n└── src\n    └── main.rs\n```\n\n- **Cargo.toml** – Project metadata and dependencies.  \n- **src/main.rs** – Core logic of the simulator: `QuantumState`, gates, measurement functions, circuit building, etc.  \n\n---\n\n## Installation and Building\n\n1. **Clone or download** this repository:\n   ```bash\n   git clone https://github.com/109de/qsim.git\n   ```\n   or download and extract the ZIP file.\n\n2. **Navigate** into the project directory:\n   ```bash\n   cd qsim\n   ```\n\n3. **Build** with Cargo:\n   ```bash\n   cargo build\n   ```\n   This fetches dependencies and compiles the project.\n\n4. **Run** the simulator example:\n   ```bash\n   cargo run\n   ```\n   You’ll see printed output describing the initial state, the final state after gates, measurement results, etc.\n\n---\n\n## Usage\n\n1. **Create a quantum state**:\n   ```rust\n   let mut qstate = QuantumState::new(3); // 3-qubit state\n   ```\n2. **Build a circuit** by adding gates:\n   ```rust\n   let mut circuit = QuantumCircuit::new(3);\n\n// Example gates:\ncircuit.add_gate(QuantumGate::new_h(), \u0026[0]);\ncircuit.add_gate(QuantumGate::new_x(), \u0026[1]);\ncircuit.add_gate(QuantumGate::new_cnot(), \u0026[0, 1]);\ncircuit.add_gate(QuantumGate::new_toffoli(), \u0026[0, 1, 2]);\n```\n3. **Apply** the circuit to the state:\n   ```rust\n   circuit.apply(\u0026mut qstate);\n   ```\n4. **Measure** qubits:\n   ```rust\n   // Measure a single qubit\n   let outcome = qstate.measure_qubit(2);\n   println!(\"Measured qubit 2: {}\", outcome);\n\n   // Or measure multiple qubits\n   let outcome_2bits = qstate.measure_qubits(\u0026[0, 1]);\n   println!(\"Measured qubits [0,1]: {:#b}\", outcome_2bits);\n\n   // Or measure all qubits\n   let final_measurement = qstate.measure_all();\n   println!(\"Measured all qubits: {:#b}\", final_measurement);\n   ```\n5. **Print** the state:\n   ```rust\n   qstate.print_state(); // Lists all amplitudes in binary basis\n   ```\n\n## Examples\n\n- **Run the simulator example directly**:\n\n  ```bash\n  cargo run --example simulator\n  ```\n\n  Output will show the **initial state** (\\(|000\\rangle\\) for 3 qubits), then the **state** after your specified gates (Hadamard, X, CNOT, etc.), and finally the results of **measurement** and the collapsed state.\n\n- **Add new gates** (like a custom 2-qubit gate with a user-defined matrix):\n\n  ```rust\n  let custom_matrix = DMatrix::\u003cComplex64\u003e::from_iterator(\n      4, 4,\n      vec![\n          Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0), /* etc... */\n          // fill in the 16 entries\n      ]\n  );\n  let custom_gate = QuantumGate::new_arbitrary(custom_matrix);\n  circuit.add_gate(custom_gate, \u0026[0, 2]); // apply to qubits 0 and 2\n  ```\n\n- **Measure subsets of qubits**:\n\n  ```rust\n  let subset_result = qstate.measure_qubits(\u0026[2, 0]);\n  // This result is an integer whose bit 0 = qubit2's measurement, bit 1 = qubit0's measurement\n  ```\n\n---\n\n## Contributing\n\n1. **Fork** this repository on GitHub.  \n2. **Create a branch** for your feature:  \n   ```bash\n   git checkout -b my-new-feature\n   ```  \n3. **Commit** your changes:  \n   ```bash\n   git commit -am \"Add a new quantum gate or feature\"\n   ```  \n4. **Push** your branch:  \n   ```bash\n   git push origin my-new-feature\n   ```  \n5. **Open a Pull Request** describing your changes, so it can be reviewed and merged.\n\n---\n\n## License\n\nYou are free to use and adapt this code in accordance with your chosen license. Commonly, open-source Rust projects are released under [MIT](https://opensource.org/licenses/MIT) or [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0). Feel free to include your own LICENSE file at the root of the repository.\n\n---\n\n\u003e **Enjoy experimenting with quantum computing in Rust!** If you have questions or suggestions, feel free to open an issue or reach out.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F10d9e%2Fqsim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F10d9e%2Fqsim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F10d9e%2Fqsim/lists"}