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

Add tfq.math.mps_1d_expectation for 1D MPS #610

Merged
merged 36 commits into from
Jan 4, 2022
Merged

Conversation

jaeyoo
Copy link
Member

@jaeyoo jaeyoo commented Aug 3, 2021

Resolve #603 1st of the three ops.

@jaeyoo jaeyoo changed the title Add tfq.math.1d_expectation for 1D MPS Add tfq.math.mps_1d_expectation for 1D MPS Aug 3, 2021
Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

This is a great first pass @jaeyoo ! Would you be able to add some tests that verify expectations values computed using cirq's built in expectation calculation functions ? Also removing some of the print statements from the code :)

tensorflow_quantum/core/src/program_resolution.cc Outdated Show resolved Hide resolved
Both Operation with natural born controlled gate (e.g. CNOT) and
synthesized ControlledOperation are converted into the same tfq_gate_set
with control_qubit. So, we can't filter ControlledOperation in this way,
revert the previous commit.
@MichaelBroughton
Copy link
Collaborator

@jaeyoo any updates on this ? (My workstation has gone offline and won't be into the office for a while to turn it back on).

@jaeyoo
Copy link
Member Author

jaeyoo commented Sep 6, 2021

@jaeyoo any updates on this ? (My workstation has gone offline and won't be into the office for a while to turn it back on).

I am still working on how to fix the delete issue...

@jaeyoo
Copy link
Member Author

jaeyoo commented Sep 30, 2021

@MichaelBroughton I could fix the error finally. It was not easy because it had multiple reasons.

  1. It turns out that we can't use tfq::QsimFor : I realized that the current MPSSimulator doesn't accept qsim::SequantialFor. It accepts qsim::For only. On top of it, for initialization, dummy value 1 is used for ForArgs, not tfq_for value.
  2. Even if the theoretical minimum of qsim MPS bond_dim is 2 (as shown here), but the real test seems to require minimum 4 (as qsim MPSSimulator test code.), because the weird double free error occurred from it. After increasing the bond_dim from 2 to 4, the error's gone. So I want to make sure if there is a good test scenario to deal with which circuit accepts bond_dim=2 or not. could you please recommend it?
  3. we can't use ApplyFusedGate() anymore. On top of it, ApplyGate() should be done with main_circuits not with fused_circuits.
  4. Likewise, we can't use ComputeExpectation. We need to add similar function using ApplyGate(), not ApplyFusedGate()

Thank you so so much for your kind patience.

@jaeyoo
Copy link
Member Author

jaeyoo commented Sep 30, 2021

Failed at //tensorflow_quantum/core/ops/noise:noisy_samples_op_test May i just re-run the test?

@jaeyoo
Copy link
Member Author

jaeyoo commented Sep 30, 2021

PTAL

Note: qsim::ApplyFusedGate will be supported after qsim==0.10.3 due to quantumlib/qsim#395.

@MichaelBroughton
Copy link
Collaborator

We just cut a release here: https://github.com/quantumlib/qsim/releases/tag/v0.10.3-dev%2B20211001

Could we try upgrading to this new qsim version ?

@jaeyoo
Copy link
Member Author

jaeyoo commented Oct 1, 2021

We just cut a release here: https://github.com/quantumlib/qsim/releases/tag/v0.10.3-dev%2B20211001

Could we try upgrading to this new qsim version ?

Sure, it's my pleasure. Done.

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

Looking great! Just a few moderate sized changes then we should be good to merge!

Comment on lines 44 to 48
bond_dim: `tf.Tensor` for an integer representing bond dimension
in this 1D MPS. This will create the following MPS:
[2, bond_dim], [bond_dim, 2, bond_dim] ... [bond_dim, 2]

The `bond_dim` should be >= 4.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't this just be a constant and doesn't have to be a tf.Tensor ?

Comment on lines 125 to 127
if (max_num_qubits >= 26 || programs.size() == 1) {
ComputeLarge(num_qubits, fused_circuits, pauli_sums, context,
&output_tensor);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since MPS simulation doesn't have such a massive memory footprint as statevector I think we don't need ComputeLarge anymore.

util.convert_to_tensor([[x] for x in pauli_sums]))
self.assertDTypeEqual(res, np.float32)

def test_simulate_mps_1d_expectation_results(self):
Copy link
Collaborator

@MichaelBroughton MichaelBroughton Oct 4, 2021

Choose a reason for hiding this comment

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

This is a good start. Could we add a few more tests (with different names) here that do the following:

  1. Test with all single qubit gates in tfq.get_supported_gates
  2. Test with all single qubit gates in tfq.get_supported_gates.controlled_by(a_qubit) and ensure that this fails or passes accordingly)
  3. Test that all two qubit gates in tfq.get_supported_gates works.
  4. Ensure that the empty case works as expected

@MichaelBroughton
Copy link
Collaborator

I went ahead and cleaned up the implementation a little bit. Notable changes:

  1. Removed the use of ApplyFusedGate. On closer inspection if two qubits that weren't adjacent to one another got fused together, things would break.
  2. Added in the swap_endian option for parsing. The mps simulator in qsim has different endianness from the state vector simulator. Working in a proper fix to qsim would take a very long time so this is a workaround until we can get this fixed. Swap endianness of MPS simulator to match that of the state vector simulator quantumlib/qsim#492
  3. Removed the ComputeLarge since MPS has a much smaller memory footprint
  4. Added a few more tests.

Copy link
Member Author

@jaeyoo jaeyoo left a comment

Choose a reason for hiding this comment

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

LGTM for me, but I can't approve my PR. :) @MichaelBroughton

@MichaelBroughton MichaelBroughton merged commit dea26c6 into master Jan 4, 2022
@MichaelBroughton MichaelBroughton deleted the add_mps_1d branch January 4, 2022 19:35
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.

[Design] Implement MPS ops under tfq.math
2 participants