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

Selecting wires of default.tensor at runtime if they are not provided by user #5744

Merged
merged 12 commits into from
May 31, 2024

Conversation

PietropaoloFrisoni
Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni commented May 27, 2024

Context: In the default.tensor device, we need to create an initial state (e.g. in the MPS case using the MPS_computational_state function) in quimb, which requires the number of qubits to be defined.

We have a very cryptic error from quimb as soon as we compute something with this device if there is a mismatch between the circuit wires and those on the device.

To circumvent this issue, we previously made wires the last positional argument for this device (as in lightning.qubit), checking at runtime if there's a mismatch between the wires on the device and those associated with the circuit.

Description of the Change: We now instantiate the quimb tensor directly in the simulate function. The wires on the device are selected at runtime to match those on the circuit in case they haven't been provided by the user.

Otherwise, default.tensor raises an informative error if the wires have been provided by the user and there is a mismatch.
This allows the wires argument to be optional for this device.

Benefits: This makes the default.tensor more flexible and, generally, more compatible with the existing devices in PL.

Possible Drawbacks: As stated above, we have an error if there is a clear mismatch, such as:

dev = qml.device("default.tensor", wires=2)
ops = [
    qml.Identity(wires=0),
    qml.Identity(wires=(0, 1)),
    qml.CNOT(wires=[0, 1]),
    qml.RX(phi=0.5, wires=0),
    qml.RY(phi=1, wires=5),
    qml.RX(phi=2, wires=1),
]
measurements = [qml.expval(qml.PauliZ(wires=15))]
tape = qml.tape.QuantumScript(ops, measurements)

>>> dev.execute(tape)
WireError: Mismatch between circuit and device wires. Circuit has wires [0, 1, 5, 15]. Tensor on device has wires [0, 1]

which seems justified. However, the error disappears if we don't provide wires:

dev = qml.device("default.tensor")
ops = [
    qml.Identity(wires=0),
    qml.Identity(wires=(0, 1)),
    qml.CNOT(wires=[0, 1]),
    qml.RX(phi=0.5, wires=0),
    qml.RY(phi=1, wires=5),
    qml.RX(phi=2, wires=1),
]
measurements = [qml.expval(qml.PauliZ(wires=15))]
tape = qml.tape.QuantumScript(ops, measurements)

dev.execute(tape)
1.0

Related GitHub Issues: None.

Related Shortcut Stories: [sc-64007]

@PietropaoloFrisoni PietropaoloFrisoni changed the title Establishing wires at runtime Selecting wires of default.tensor at runtime if they are not provided by user May 27, 2024
Copy link

codecov bot commented May 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.66%. Comparing base (2a36a35) to head (86e30db).
Report is 258 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5744      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.01%     
==========================================
  Files         413      413              
  Lines       39254    38957     -297     
==========================================
- Hits        39126    38828     -298     
- Misses        128      129       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@PietropaoloFrisoni PietropaoloFrisoni marked this pull request as ready for review May 28, 2024 17:17
Copy link
Member

@multiphaseCFD multiphaseCFD left a comment

Choose a reason for hiding this comment

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

Thanks @PietropaoloFrisoni ! Just a few a comments and questions.

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Thanks Pietro!

Copy link
Member

@multiphaseCFD multiphaseCFD left a comment

Choose a reason for hiding this comment

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

…o Selecting_wires_at_runtime_default_tensor
@PietropaoloFrisoni PietropaoloFrisoni enabled auto-merge (squash) May 31, 2024 21:38
@PietropaoloFrisoni PietropaoloFrisoni merged commit ec0ebd6 into master May 31, 2024
40 checks passed
@PietropaoloFrisoni PietropaoloFrisoni deleted the Selecting_wires_at_runtime_default_tensor branch May 31, 2024 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants