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

DensityMatrixSimulator does not allow specifying initial state vector as an array #5193

Closed
maffoo opened this issue Apr 4, 2022 · 2 comments
Labels
kind/bug-report Something doesn't seem to work.

Comments

@maffoo
Copy link
Contributor

maffoo commented Apr 4, 2022

Description of the issue

How to reproduce the issue

>>> sim = cirq.DensityMatrixSimulator()
>>> circuit = cirq.Circuit(cirq.measure(cirq.LineQubit(0)))
>>> sim.simulate(circuit, initial_state=np.array([1, 0]))
...
ValueError: cannot reshape array of size 2 into shape (2,2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 sim.simulate(circuit, initial_state=np.array([1, 0]))

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/simulator.py:518, in SimulatesFinalState.simulate(self, program, param_resolver, qubit_order, initial_state)
    493 def simulate(
    494     self,
    495     program: 'cirq.AbstractCircuit',
   (...)
    498     initial_state: Any = None,
    499 ) -> TSimulationTrialResult:
    500     """Simulates the supplied Circuit.
    501 
    502     This method returns a result which allows access to the entire
   (...)
    516         SimulationTrialResults for the simulation. Includes the final state.
    517     """
--> 518     return self.simulate_sweep(
    519         program, study.ParamResolver(param_resolver), qubit_order, initial_state
    520     )[0]

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/simulator.py:533, in SimulatesFinalState.simulate_sweep(self, program, params, qubit_order, initial_state)
    522 def simulate_sweep(
    523     self,
    524     program: 'cirq.AbstractCircuit',
   (...)
    527     initial_state: Any = None,
    528 ) -> List[TSimulationTrialResult]:
    529     """Wraps computed states in a list.
    530 
    531     Prefer overriding `simulate_sweep_iter`.
    532     """
--> 533     return list(self.simulate_sweep_iter(program, params, qubit_order, initial_state))

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/simulator_base.py:345, in SimulatorBase.simulate_sweep_iter(self, program, params, qubit_order, initial_state)
    343 qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for(program.all_qubits())
    344 initial_state = 0 if initial_state is None else initial_state
--> 345 sim_state = self._create_act_on_args(initial_state, qubits)
    346 prefix, suffix = (
    347     split_into_matching_protocol_then_general(program, sweep_prefixable)
    348     if self._can_be_in_run_prefix(self.noise)
    349     else (program[0:0], program)
    350 )
    351 step_result = None

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/simulator_base.py:380, in SimulatorBase._create_act_on_args(self, initial_state, qubits)
    378         initial_state = int(initial_state / q.dimension)
    379 else:
--> 380     args = self._create_partial_act_on_args(
    381         initial_state=initial_state,
    382         qubits=qubits,
    383         classical_data=classical_data,
    384     )
    385     for q in qubits:
    386         args_map[q] = args

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/density_matrix_simulator.py:221, in DensityMatrixSimulator._create_partial_act_on_args(self, initial_state, qubits, classical_data)
    212 if self._ignore_measurement_results:
    213     return act_on_density_matrix_args.ActOnDensityMatrixArgs(
    214         qubits=qubits,
    215         prng=self._prng,
   (...)
    219         dtype=self._dtype,
    220     )
--> 221 return act_on_density_matrix_args.ActOnDensityMatrixArgs(
    222     qubits=qubits,
    223     prng=self._prng,
    224     classical_data=classical_data,
    225     initial_state=initial_state,
    226     dtype=self._dtype,
    227 )

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/_compat.py:330, in deprecated_parameter.<locals>.decorator.<locals>.decorated_func(*args, **kwargs)
    322     qualname = func.__qualname__ if func_name is None else func_name
    323     _warn_or_error(
    324         f'The {parameter_desc} parameter of {qualname} was '
    325         f'used but is deprecated.\n'
    326         f'It will be removed in cirq {deadline}.\n'
    327         f'{fix}\n',
    328     )
--> 330 return func(*args, **kwargs)

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/_compat.py:330, in deprecated_parameter.<locals>.decorator.<locals>.decorated_func(*args, **kwargs)
    322     qualname = func.__qualname__ if func_name is None else func_name
    323     _warn_or_error(
    324         f'The {parameter_desc} parameter of {qualname} was '
    325         f'used but is deprecated.\n'
    326         f'It will be removed in cirq {deadline}.\n'
    327         f'{fix}\n',
    328     )
--> 330 return func(*args, **kwargs)

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/_compat.py:330, in deprecated_parameter.<locals>.decorator.<locals>.decorated_func(*args, **kwargs)
    322     qualname = func.__qualname__ if func_name is None else func_name
    323     _warn_or_error(
    324         f'The {parameter_desc} parameter of {qualname} was '
    325         f'used but is deprecated.\n'
    326         f'It will be removed in cirq {deadline}.\n'
    327         f'{fix}\n',
    328     )
--> 330 return func(*args, **kwargs)

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/act_on_density_matrix_args.py:307, in ActOnDensityMatrixArgs.__init__(self, target_tensor, available_buffer, qid_shape, prng, log_of_measurement_results, qubits, ignore_measurement_results, initial_state, dtype, classical_data)
    242 @_compat.deprecated_parameter(
    243     deadline='v0.15',
    244     fix='Use classical_data.',
   (...)
    271     classical_data: Optional['cirq.ClassicalDataStore'] = None,
    272 ):
    273     """Inits ActOnDensityMatrixArgs.
    274 
    275     Args:
   (...)
    305             and `qid_shape` is not provided.
    306     """
--> 307     state = _BufferedDensityMatrix.create(
    308         initial_state=target_tensor if target_tensor is not None else initial_state,
    309         qid_shape=tuple(q.dimension for q in qubits) if qubits is not None else None,
    310         dtype=dtype,
    311         buffer=available_buffer,
    312     )
    313     if ignore_measurement_results:
    314         super().__init__(
    315             state=state,
    316             prng=prng,
   (...)
    320             classical_data=classical_data,
    321         )

File ~/.virtualenvs/pyle/lib/python3.8/site-packages/cirq/sim/act_on_density_matrix_args.py:82, in _BufferedDensityMatrix.create(cls, initial_state, qid_shape, dtype, buffer)
     80 else:
     81     if qid_shape is not None:
---> 82         density_matrix = initial_state.reshape(qid_shape * 2)
     83     else:
     84         density_matrix = initial_state

ValueError: cannot reshape array of size 2 into shape (2,2)

Cirq version

0.15.0.dev20220402003018

Note that passing a state vector as an array to cirq.to_valid_density_matrix does work, so it seems cirq.DensityMatrixSimulator is not using that.

@maffoo maffoo added the kind/bug-report Something doesn't seem to work. label Apr 4, 2022
@daxfohl
Copy link
Contributor

daxfohl commented Apr 4, 2022

Is this same as #3958?

@maffoo
Copy link
Contributor Author

maffoo commented Apr 4, 2022

Yeah, basically the same thing. Closing this as a dupe.

@maffoo maffoo closed this as completed Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug-report Something doesn't seem to work.
Projects
None yet
Development

No branches or pull requests

2 participants