feat: add default zero state to QubitCircuit.run()#376
Conversation
tests/test_circuit.py
Outdated
| """Test that qc.run() without a state argument defaults to |00...0>.""" | ||
| # Single qubit: X gate should map |0> to |1> | ||
| qc = QubitCircuit(num_qubits=1) | ||
| qc.add_gate("X", targets=0) |
There was a problem hiding this comment.
| qc.add_gate("X", targets=0) | |
| qc.add_gate(X, targets=0) |
tests/test_circuit.py
Outdated
|
|
||
| # Two qubits: default |00>, apply X on qubit 0, expect |10> | ||
| qc2 = QubitCircuit(num_qubits=2) | ||
| qc2.add_gate("X", targets=0) |
tests/test_circuit.py
Outdated
| qc = QubitCircuit(num_qubits=1) | ||
| qc.add_gate("X", targets=0) | ||
| result = qc.run() | ||
| fid = pytest.approx(qutip.fidelity(result, basis(2, 1))) |
There was a problem hiding this comment.
Please use relative and absolute tolerances
Mayank447
left a comment
There was a problem hiding this comment.
Just some minor changes.
@satishkc7 In case you used AI tools, we expect you to mention that in the PR description as per our Contribution guidelines.
tests/test_circuit.py
Outdated
| fid = pytest.approx(qutip.fidelity(result, basis(2, 1)), rel=1e-6, abs=1e-6) | ||
| assert fid == 1.0 |
There was a problem hiding this comment.
| fid = pytest.approx(qutip.fidelity(result, basis(2, 1)), rel=1e-6, abs=1e-6) | |
| assert fid == 1.0 | |
| assert qutip.fidelity(result, basis(2, 1)) == pytest.approx(1.0, rel=1e-6, abs=1e-6) |
tests/test_circuit.py
Outdated
| fid2 = pytest.approx(qutip.fidelity(result2, expected), rel=1e-6, abs=1e-6) | ||
| assert fid2 == 1.0 |
|
Please run the test locally and confirm that it passes before raising a PR and please state explicitly if and how AI tools are used. |
|
Thanks for the review feedback. All requested changes have been addressed:
Tests confirmed passing locally: Regarding AI tools - yes, I used Claude (Claude Code) to assist with drafting the implementation and tests. Happy to note this in the PR description as well if needed. |
When no state is provided, qc.run() now defaults to the all-zero ket state |00...0> constructed from the circuit's qubit dimensions. Resolves qutip#374.
- Use gates.X object instead of string literal - Add rel and abs tolerances to pytest.approx calls
fb5000f to
5700af3
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates QubitCircuit.run() to make the state argument optional, defaulting to the all-zero computational basis ket for the circuit’s configured subsystem dimensions. This aligns the API with the common workflow where circuits are run from |00…0⟩ unless otherwise specified.
Changes:
- Make
QubitCircuit.run(state=...)acceptstate=Noneand synthesize the default initial ket fromself.dims. - Update the
run()docstring to describe the new default behavior. - Add tests covering the default initial state for 1- and 2-qubit circuits.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/qutip_qip/circuit/circuit.py |
Defaults run()’s input state to the all-zero ket constructed from self.dims when omitted. |
tests/test_circuit.py |
Adds regression tests ensuring run() without state starts from |00…0⟩ and produces expected outputs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@BoxiLi You can merge this PR. Do a squash merge btw. |
Closes #374.
Summary
qc.run()previously required an explicitstateargument in every call. In the large majority of use cases the initial state is the all-zero ket |00...0>. This PR makesstateoptional by defaulting to that state when none is provided.Changes
circuit.py: addbasisandtensorto the qutip import, changestatetostate=None, and insert a one-liner that constructs the zero state fromself.dimswhen the argument is omitted.test_circuit.py: addtest_run_default_zero_statecovering a single-qubit and a two-qubit circuit.Before
After
Passing an explicit state still works exactly as before.