Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the basis gates of BasicSimulator #12186

Merged
merged 19 commits into from
Apr 30, 2024
Merged

Conversation

1ucian0
Copy link
Member

@1ucian0 1ucian0 commented Apr 16, 2024

Fixes #10852

The simulator BasicSimulator is python-based simulator included in qiskit.providers.basic_provider. After the discussion in #10673, Qiskit resolved that every standard gate, up to 3 qubits, can be in BasicSimulator.

The new basis support was extended to: , ccx, ccz, ch, cp, crx, cry, crz, cs, csdg, cswap, csx, cu, cu1, cu3, cx, cy, cz, dcx, delay, ecr, global_phase, h, id, iswap, measure, p, r, rccx, reset, rx, rxx, ry, ryy, rz, rzx, rzz, s, sdg, swap, sx, sxdg, t, tdg, u, u1, u2, u3, unitary, x, xx_minus_yy, xx_plus_yy, y, and z.

@1ucian0 1ucian0 requested review from jyu00 and a team as code owners April 16, 2024 11:12
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

@1ucian0 1ucian0 added the Changelog: New Feature Include in the "Added" section of the changelog label Apr 16, 2024
@1ucian0 1ucian0 added this to the 1.1.0 milestone Apr 16, 2024
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @1ucian0, would it make sense to add a few tests to check that the extended support works?

@coveralls
Copy link

coveralls commented Apr 19, 2024

Pull Request Test Coverage Report for Build 8853263480

Details

  • 28 of 28 (100.0%) changed or added relevant lines in 2 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.1%) to 89.327%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.11%
Totals Coverage Status
Change from base Build 8851171785: -0.1%
Covered Lines: 35980
Relevant Lines: 40279

💛 - Coveralls

if operation.name == "unitary":
qubits = operation.qubits
gate = operation.params[0]
self._add_unitary(gate, qubits)
elif operation.name in ("id", "u0", "delay"):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fully sure that delay is the same as id in BasicSimulator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that BasicSimulator doesn't understand pulses, I think that the choice is between accepting delay and treating it as an identity or failing if a circuit contains a delay. I would vote for accepting it but raising a user warning to err on the side of caution, given that users often don't understand the capabilities of each simulator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakelishman what do you think? Should be delay be id, or warning, or not accepted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic simulator is noiseless, and in noiseless quantum-state evolution a time-delay is the identity op. I think it's fine / correct to accept delay and do nothing.

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @1ucian0 for adding the tests and handling the edge cases. The PR looks good to me, I just have two minor suggestions. One is an inline suggestion about raising a user warning with delay, and the other one would be to include a transpilation step in the run tests, just to make sure that the pass manager runs successfully in case somebody chooses a BasicSimulator as target. I checked out your PR locally and confirmed that it works so this can also be left to a follow-up.

if operation.name == "unitary":
qubits = operation.qubits
gate = operation.params[0]
self._add_unitary(gate, qubits)
elif operation.name in ("id", "u0", "delay"):
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a message like this one?

Suggested change
pass
if operation.name == "delay":
warnings.warn(
"BasicSimulator cannot simulate gate durations, "
"treating delay operation as an identity.",
UserWarning,
stacklevel=2,
)
pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're fine without a warning; it's completely valid to simulate a delay as an identity if there's no noise in the system.

@1ucian0
Copy link
Member Author

1ucian0 commented Apr 23, 2024

the other one would be to include a transpilation step in the run tests, just to make sure that the pass manager runs successfully in case somebody chooses a BasicSimulator as target.

In e1ad7cb I added the conversion to target. I feel that checking transpilation is checking too much. If the backend can convert to a target and transpile does something wrong with it, it is a different issue.

ElePT
ElePT previously approved these changes Apr 25, 2024
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#12186 (comment) testing the target seems fair. I am also not too worried about delay because I think that treating it as id is the closest we can get with a statevector simulator (so in any case it isn't wrong in my opinion), so in the interest of time I am going to approve.

Comment on lines 587 to 588
ctrl_state: control state expressed as integer,
string (e.g.``'110'``), or ``None``. If ``None``, use all 1s.
ctrl_state: control state expressed as integer, string (e.g.``"110"``), or ``None``.
If ``None``, use all 1s.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have broken the formatting because the continuation line is unindented, and I don't think it'll have actually fixed the problem, which is that there's no space betwen e.g. and the opening ``.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try in c35b9cf

Comment on lines 43 to 77

if gate == "U":
if gate in ("U", "u"):
gc = gates.UGate
elif gate == "u1":
gc = gates.U1Gate
elif gate == "u2":
gc = gates.U2Gate
elif gate == "u3":
gc = gates.U3Gate
elif gate == "h":
gc = gates.HGate
elif gate == "u":
gc = gates.UGate
elif gate == "p":
gc = gates.PhaseGate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to add this many new gates, please can we switch to using a dict lookup to retrieve the gate classes?

Copy link
Member Author

@1ucian0 1ucian0 Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5cddee7

if operation.name == "unitary":
qubits = operation.qubits
gate = operation.params[0]
self._add_unitary(gate, qubits)
elif operation.name in ("id", "u0", "delay"):
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're fine without a warning; it's completely valid to simulate a delay as an identity if there's no noise in the system.

Comment on lines 2 to 5
upgrade_providers:
- |
The :class:`.BasicSimulator` python-based simulator included in :mod:`qiskit.providers.basic_provider`
now includes all the standard gates (:mod:`qiskit.circuit.library .standard_gates`) up to 3 qubits. The new basis support includes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is maybe a feature rather than an upgrade? Fwiw, I probably wouldn't bother listing all the gates below - "includes all the standard gates in :mod:`qiskit.circuit.library`" is descriptive enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in e81740e

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you have implemented all of @jakelishman's comments, so I will approve again and wait to see if he's got any final remark.

@ElePT ElePT added this pull request to the merge queue Apr 30, 2024
Merged via the queue into Qiskit:main with commit 7ec2c56 Apr 30, 2024
13 checks passed
ElePT pushed a commit to ElePT/qiskit that referenced this pull request May 31, 2024
* extend the basis gates of BasicSimulator

* remove variational sized gates

* reno and test

* accidentally missing u2 and u1

* three qubits support

* support for parameters

* support for all the standard gates upto 3 qubits

* test

* adding rccx

* missing gates

* test notes

* readjust test

* check target

* tt

* dict lookup

* reno
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

extend BasicSimulator with standard gates
5 participants