def test_quantum_state_computational_basis_state(): state = cirq.quantum_state(7, qid_shape=(3, 4)) np.testing.assert_allclose( state.data, cirq.one_hot(index=7, shape=(12, ), dtype=np.complex64)) assert state.qid_shape == (3, 4) assert state.dtype == np.complex64 state = cirq.quantum_state((0, 1, 2, 3), qid_shape=(1, 2, 3, 4), dtype=np.complex128) np.testing.assert_allclose( state.data, cirq.one_hot(index=(0, 1, 2, 3), shape=(1, 2, 3, 4), dtype=np.complex64)) assert state.qid_shape == (1, 2, 3, 4) assert state.dtype == np.complex128 with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state(7) with pytest.raises(ValueError, match='out of range'): _ = cirq.quantum_state(7, qid_shape=(2, 2)) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state((0, 1, 2, 3)) with pytest.raises(ValueError, match='out of bounds'): _ = cirq.quantum_state((0, 1, 2, 3), qid_shape=(2, 2, 2, 2)) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state((0, 0, 1, 1), qid_shape=(1, 1, 2, 2))
def test_reset_act_on(): with pytest.raises(TypeError, match="Failed to act"): cirq.act_on(cirq.ResetChannel(), object()) args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(index=(1, 1, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64), available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), axes=[1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(cirq.ResetChannel(), args) assert args.log_of_measurement_results == {} np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(1, 0, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64), ) cirq.act_on(cirq.ResetChannel(), args) assert args.log_of_measurement_results == {} np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(1, 0, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64), )
def test_reset_act_on(): with pytest.raises(TypeError, match="Failed to act"): cirq.act_on(cirq.ResetChannel(), DummyActOnArgs(), qubits=()) args = cirq.ActOnStateVectorArgs( available_buffer=np.empty(shape=(2, 2, 2, 2, 2), dtype=np.complex64), qubits=cirq.LineQubit.range(5), prng=np.random.RandomState(), log_of_measurement_results={}, initial_state=cirq.one_hot(index=(1, 1, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(cirq.ResetChannel(), args, [cirq.LineQubit(1)]) assert args.log_of_measurement_results == {} np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(1, 0, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64), ) cirq.act_on(cirq.ResetChannel(), args, [cirq.LineQubit(1)]) assert args.log_of_measurement_results == {} np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(1, 0, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64), )
def _kraus_(self): bottom_right = cirq.one_hot(index=(3, 3), shape=(4, 4), dtype=np.complex64) top_right = cirq.one_hot(index=(0, 3), shape=(4, 4), dtype=np.complex64) return [ np.eye(4) * np.sqrt(3 / 4), (np.eye(4) - bottom_right) * np.sqrt(1 / 4), top_right * np.sqrt(1 / 4), ]
def test_to_valid_density_matrix_from_density_matrix_tensor(): np.testing.assert_almost_equal( cirq.to_valid_density_matrix(cirq.one_hot(shape=(2, 2, 2, 2, 2, 2), dtype=np.complex64), num_qubits=3), cirq.one_hot(shape=(8, 8), dtype=np.complex64), ) np.testing.assert_almost_equal( cirq.to_valid_density_matrix(cirq.one_hot(shape=(2, 3, 4, 2, 3, 4), dtype=np.complex64), qid_shape=(2, 3, 4)), cirq.one_hot(shape=(24, 24), dtype=np.complex64), )
def test_one_hot(): result = cirq.one_hot(shape=4, dtype=np.int32) assert result.dtype == np.int32 np.testing.assert_array_equal(result, [1, 0, 0, 0]) np.testing.assert_array_equal( cirq.one_hot(shape=[2, 3], dtype=np.complex64), [[1, 0, 0], [0, 0, 0]]) np.testing.assert_array_equal( cirq.one_hot(shape=[2, 3], dtype=np.complex64, index=(0, 2)), [[0, 0, 1], [0, 0, 0]]) np.testing.assert_array_equal( cirq.one_hot(shape=5, dtype=np.complex128, index=3), [0, 0, 0, 1, 0])
def test_quantum_state(): state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex128) state_tensor_1 = np.reshape(state_vector_1, (2, 2)) density_matrix_1 = np.outer(state_vector_1, np.conj(state_vector_1)) state = cirq.QuantumState(state_vector_1, qid_shape=(2, 2)) assert state.data is state_vector_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128 np.testing.assert_array_equal(state.state_vector(), state_vector_1) np.testing.assert_array_equal(state.state_tensor(), state_tensor_1) np.testing.assert_array_equal(state.density_matrix(), density_matrix_1) np.testing.assert_array_equal(state.state_vector_or_density_matrix(), state_vector_1) state = cirq.QuantumState(state_tensor_1, qid_shape=(2, 2)) assert state.data is state_tensor_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128 np.testing.assert_array_equal(state.state_vector(), state_vector_1) np.testing.assert_array_equal(state.state_tensor(), state_tensor_1) np.testing.assert_array_equal(state.density_matrix(), density_matrix_1) np.testing.assert_array_equal(state.state_vector_or_density_matrix(), state_vector_1) state = cirq.QuantumState(density_matrix_1, qid_shape=(2, 2)) assert state.data is density_matrix_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128 assert state.state_vector() is None assert state.state_tensor() is None np.testing.assert_array_equal(state.density_matrix(), density_matrix_1) np.testing.assert_array_equal(state.state_vector_or_density_matrix(), density_matrix_1)
def test_with_registers(): circuit = quirk_url_to_circuit( 'https://algassert.com/quirk#circuit={"cols":' '[' '[{"id":"setA","arg":3}],' '["+=AB3",1,1,"inputB2"]' ']}') op = cast(cirq.ArithmeticOperation, circuit[0].operations[0]) with pytest.raises(ValueError, match='number of registers'): _ = op.with_registers() with pytest.raises(ValueError, match='first register.*mutable target'): _ = op.with_registers(1, 2, 3) op2 = op.with_registers([], 5, 5) np.testing.assert_allclose(cirq.unitary(cirq.Circuit(op2)), np.array([[1]]), atol=1e-8) op2 = op.with_registers([*cirq.LineQubit.range(3)], 5, 5) np.testing.assert_allclose(cirq.final_state_vector(cirq.Circuit(op2), initial_state=0), cirq.one_hot(index=25 % 8, shape=8, dtype=np.complex64), atol=1e-8)
def test_decomposed_fallback(): class Composite(cirq.Gate): def num_qubits(self) -> int: return 1 def _decompose_(self, qubits): yield cirq.X(*qubits) qid_shape = (2, ) tensor = cirq.to_valid_density_matrix(0, len(qid_shape), qid_shape=qid_shape, dtype=np.complex64) args = cirq.ActOnDensityMatrixArgs( target_tensor=tensor, available_buffer=[np.empty_like(tensor) for _ in range(3)], qubits=cirq.LineQubit.range(1), prng=np.random.RandomState(), log_of_measurement_results={}, qid_shape=qid_shape, ) cirq.act_on(Composite(), args, cirq.LineQubit.range(1)) np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(1, 1), shape=(2, 2), dtype=np.complex64))
def test_act_on_state_vector(): a, b = [cirq.LineQubit(3), cirq.LineQubit(1)] m = cirq.measure(a, b, key='out', invert_mask=(True, ), confusion_map={(1, ): np.array([[0, 1], [1, 0]])}) args = cirq.StateVectorSimulationState( available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), qubits=cirq.LineQubit.range(5), prng=np.random.RandomState(), initial_state=cirq.one_hot(shape=(2, 2, 2, 2, 2), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [1, 1]} args = cirq.StateVectorSimulationState( available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), qubits=cirq.LineQubit.range(5), prng=np.random.RandomState(), initial_state=cirq.one_hot(index=(0, 1, 0, 0, 0), shape=(2, 2, 2, 2, 2), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [1, 0]} args = cirq.StateVectorSimulationState( available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), qubits=cirq.LineQubit.range(5), prng=np.random.RandomState(), initial_state=cirq.one_hot(index=(0, 1, 0, 1, 0), shape=(2, 2, 2, 2, 2), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(m, args) datastore = cast(cirq.ClassicalDataDictionaryStore, args.classical_data) out = cirq.MeasurementKey('out') assert args.log_of_measurement_results == {'out': [0, 0]} assert datastore.records[out] == [(0, 0)] cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [0, 0]} assert datastore.records[out] == [(0, 0), (0, 0)]
def test_act_on_qutrit(): a, b = [cirq.LineQid(3, dimension=3), cirq.LineQid(1, dimension=3)] m = cirq.measure( a, b, key='out', invert_mask=(True, ), confusion_map={(1, ): np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])}, ) args = cirq.StateVectorSimulationState( available_buffer=np.empty(shape=(3, 3, 3, 3, 3)), qubits=cirq.LineQid.range(5, dimension=3), prng=np.random.RandomState(), initial_state=cirq.one_hot(index=(0, 2, 0, 2, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [2, 0]} args = cirq.StateVectorSimulationState( available_buffer=np.empty(shape=(3, 3, 3, 3, 3)), qubits=cirq.LineQid.range(5, dimension=3), prng=np.random.RandomState(), initial_state=cirq.one_hot(index=(0, 1, 0, 2, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [2, 2]} args = cirq.StateVectorSimulationState( available_buffer=np.empty(shape=(3, 3, 3, 3, 3)), qubits=cirq.LineQid.range(5, dimension=3), prng=np.random.RandomState(), initial_state=cirq.one_hot(index=(0, 2, 0, 1, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [0, 0]}
def test_act_on_gate(): args = ccq.mps_simulator.MPSState(qubits=cirq.LineQubit.range(3), prng=np.random.RandomState(0)) cirq.act_on(cirq.X, args, [cirq.LineQubit(1)]) np.testing.assert_allclose( args.state_vector().reshape((2, 2, 2)), cirq.one_hot(index=(0, 1, 0), shape=(2, 2, 2), dtype=np.complex64), )
def test_act_on(): a, b = cirq.LineQubit.range(2) m = cirq.measure(a, b, key='out', invert_mask=(True, )) with pytest.raises(TypeError, match="Failed to act"): cirq.act_on(m, object()) args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(shape=(2, 2, 2, 2, 2), dtype=np.complex64), available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), axes=[3, 1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [1, 0]} args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(index=(0, 1, 0, 0, 0), shape=(2, 2, 2, 2, 2), dtype=np.complex64), available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), axes=[3, 1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [1, 1]} args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(index=(0, 1, 0, 1, 0), shape=(2, 2, 2, 2, 2), dtype=np.complex64), available_buffer=np.empty(shape=(2, 2, 2, 2, 2)), axes=[3, 1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [0, 1]} with pytest.raises(ValueError, match="already logged to key"): cirq.act_on(m, args)
def test_decomposed_fallback(): class Composite(cirq.Gate): def num_qubits(self) -> int: return 1 def _decompose_(self, qubits): yield cirq.X(*qubits) args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(shape=(2, 2, 2), dtype=np.complex64), available_buffer=np.empty((2, 2, 2), dtype=np.complex64), axes=[1], prng=np.random.RandomState(), log_of_measurement_results={}) cirq.act_on(Composite(), args) np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(0, 1, 0), shape=(2, 2, 2), dtype=np.complex64))
def test_decomposed_fallback(): class Composite(cirq.Gate): def num_qubits(self) -> int: return 1 def _decompose_(self, qubits): yield cirq.X(*qubits) args = cirq.StateVectorSimulationState( available_buffer=np.empty((2, 2, 2), dtype=np.complex64), qubits=cirq.LineQubit.range(3), prng=np.random.RandomState(), initial_state=cirq.one_hot(shape=(2, 2, 2), dtype=np.complex64), dtype=np.complex64, ) cirq.act_on(Composite(), args, [cirq.LineQubit(1)]) np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(0, 1, 0), shape=(2, 2, 2), dtype=np.complex64))
def test_density_matrix(): density_matrix_1 = np.eye(4, dtype=np.complex64) / 4 state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex64) state = cirq.density_matrix(density_matrix_1) assert state.data is density_matrix_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex64 with pytest.raises(ValueError, match='square'): _ = cirq.density_matrix(state_vector_1)
def test_default_parameter(): dtype = np.complex64 tensor = cirq.one_hot(shape=(2, 2, 2), dtype=np.complex64) qubits = cirq.LineQubit.range(3) args = cirq.StateVectorSimulationState(qubits=qubits, initial_state=tensor, dtype=dtype) qid_shape = cirq.protocols.qid_shape(qubits) tensor = np.reshape(tensor, qid_shape) np.testing.assert_almost_equal(args.target_tensor, tensor) assert args.available_buffer.shape == tensor.shape assert args.available_buffer.dtype == tensor.dtype
def test_quantum_state_state_vector_state_tensor(): state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex128) state_tensor_1 = np.reshape(state_vector_1, (2, 2)) state = cirq.quantum_state(state_vector_1, dtype=np.complex64, qid_shape=(2, 2)) np.testing.assert_array_equal(state.data, state_vector_1) assert state.qid_shape == (2, 2) assert state.dtype == np.complex64 state = cirq.quantum_state(state_tensor_1, qid_shape=(2, 2)) assert state.data is state_tensor_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128
def test_cannot_act(): class NoDetails: pass args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(shape=(2, 2, 2), dtype=np.complex64), available_buffer=np.empty((2, 2, 2), dtype=np.complex64), axes=[1], prng=np.random.RandomState(), log_of_measurement_results={}) with pytest.raises(TypeError, match="Failed to act"): cirq.act_on(NoDetails(), args)
def test_act_on_qutrit(): a, b = cirq.LineQid.range(2, dimension=3) m = cirq.measure(a, b, key='out', invert_mask=(True, )) args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(index=(0, 2, 0, 2, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64), available_buffer=np.empty(shape=(3, 3, 3, 3, 3)), axes=[3, 1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [2, 2]} args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(index=(0, 1, 0, 2, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64), available_buffer=np.empty(shape=(3, 3, 3, 3, 3)), axes=[3, 1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [2, 1]} args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(index=(0, 2, 0, 1, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64), available_buffer=np.empty(shape=(3, 3, 3, 3, 3)), axes=[3, 1], prng=np.random.RandomState(), log_of_measurement_results={}, ) cirq.act_on(m, args) assert args.log_of_measurement_results == {'out': [0, 2]}
def test_cannot_act(): class NoDetails: pass args = cirq.ActOnStateVectorArgs( target_tensor=cirq.one_hot(shape=(2, 2, 2), dtype=np.complex64), available_buffer=np.empty((2, 2, 2), dtype=np.complex64), qubits=cirq.LineQubit.range(3), prng=np.random.RandomState(), log_of_measurement_results={}, ) with pytest.raises(TypeError, match="Can't simulate operations"): cirq.act_on(NoDetails(), args, qubits=())
def test_cannot_act(): class NoDetails: pass args = cirq.StateVectorSimulationState( available_buffer=np.empty((2, 2, 2), dtype=np.complex64), qubits=cirq.LineQubit.range(3), prng=np.random.RandomState(), initial_state=cirq.one_hot(shape=(2, 2, 2), dtype=np.complex64), dtype=np.complex64, ) with pytest.raises(TypeError, match="Can't simulate operations"): cirq.act_on(NoDetails(), args, qubits=())
def test_monte_carlo_on_unknown_channel(): class Reset11To00(cirq.Gate): def num_qubits(self) -> int: return 2 def _kraus_(self): return [ np.eye(4) - cirq.one_hot(index=(3, 3), shape=(4, 4), dtype=np.complex64), cirq.one_hot(index=(0, 3), shape=(4, 4), dtype=np.complex64), ] for k in range(4): out = cirq.Simulator().simulate( cirq.Circuit(Reset11To00().on(*cirq.LineQubit.range(2))), initial_state=k ) np.testing.assert_allclose( out.state_vector(), cirq.one_hot(index=k % 3, shape=4, dtype=np.complex64), atol=1e-8 )
def test_quantum_state_state_vector_state_tensor(): state_vector_1 = cirq.one_hot(shape=(4, ), dtype=np.complex128) state_tensor_1 = np.reshape(state_vector_1, (2, 2)) state = cirq.quantum_state(state_vector_1, dtype=np.complex64) np.testing.assert_array_equal(state.data, state_vector_1) assert state.qid_shape == (2, 2) assert state.dtype == np.complex64 state = cirq.quantum_state(state_tensor_1, qid_shape=(2, 2)) assert state.data is state_tensor_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128 with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state(state_tensor_1) with pytest.raises(ValueError, match='not compatible'): _ = cirq.quantum_state(state_tensor_1, qid_shape=(2, 3))
def test_decomposed_fallback(): class Composite(cirq.Gate): def num_qubits(self) -> int: return 1 def _decompose_(self, qubits): yield cirq.X(*qubits) args = cirq.DensityMatrixSimulationState( qubits=cirq.LineQubit.range(1), prng=np.random.RandomState(), initial_state=0, dtype=np.complex64, ) cirq.act_on(Composite(), args, cirq.LineQubit.range(1)) np.testing.assert_allclose( args.target_tensor, cirq.one_hot(index=(1, 1), shape=(2, 2), dtype=np.complex64))
def test_quantum_state_quantum_state(): state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex128) quantum_state = cirq.QuantumState(state_vector_1, qid_shape=(2, 2)) state = cirq.quantum_state(quantum_state) assert state is quantum_state assert state.data is quantum_state.data assert state.dtype == np.complex128 state = cirq.quantum_state(quantum_state, copy=True) assert state is not quantum_state assert state.data is not quantum_state.data assert state.dtype == np.complex128 state = cirq.quantum_state(quantum_state, dtype=np.complex64) assert state is not quantum_state assert state.data is not quantum_state.data assert state.dtype == np.complex64 with pytest.raises(ValueError, match='qid shape'): state = cirq.quantum_state(quantum_state, qid_shape=(4,))
def test_infer_qid_shape(): computational_basis_state_1 = [0, 0, 0, 1] computational_basis_state_2 = [0, 1, 2, 3] computational_basis_state_3 = [0, 1, 2, 4] computational_basis_state_4 = 9 computational_basis_state_5 = [0, 1, 2, 4, 5] state_vector_1 = cirq.one_hot(shape=(4, ), dtype=np.complex64) state_vector_2 = cirq.one_hot(shape=(24, ), dtype=np.complex64) state_tensor_1 = np.reshape(state_vector_1, (2, 2)) state_tensor_2 = np.reshape(state_vector_2, (1, 2, 3, 4)) density_matrix_1 = np.eye(4, dtype=np.complex64) / 4 density_matrix_2 = np.eye(24, dtype=np.complex64) / 24 q0, q1 = cirq.LineQubit.range(2) product_state_1 = cirq.KET_PLUS(q0) * cirq.KET_PLUS(q1) assert cirq.qis.infer_qid_shape( computational_basis_state_1, state_vector_1, state_tensor_1, density_matrix_1, product_state_1, ) == (2, 2) assert cirq.qis.infer_qid_shape( product_state_1, density_matrix_1, state_tensor_1, state_vector_1, computational_basis_state_1, ) == (2, 2) assert cirq.qis.infer_qid_shape( computational_basis_state_1, computational_basis_state_2, computational_basis_state_4, state_tensor_2, ) == (1, 2, 3, 4) assert cirq.qis.infer_qid_shape(state_vector_2, density_matrix_2, computational_basis_state_4) == (24, ) assert cirq.qis.infer_qid_shape(state_tensor_2, density_matrix_2) == (1, 2, 3, 4) assert cirq.qis.infer_qid_shape(computational_basis_state_4) == (10, ) assert cirq.qis.infer_qid_shape(15, 7, 22, 4) == (23, ) with pytest.raises(ValueError, match='No states were specified'): _ = cirq.qis.infer_qid_shape() with pytest.raises(ValueError, match='Failed'): _ = cirq.qis.infer_qid_shape(computational_basis_state_1, computational_basis_state_5) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.qis.infer_qid_shape(computational_basis_state_1) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.qis.infer_qid_shape(state_tensor_1) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.qis.infer_qid_shape(density_matrix_1) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.qis.infer_qid_shape(computational_basis_state_1, computational_basis_state_2) with pytest.raises(ValueError, match='Failed'): _ = cirq.qis.infer_qid_shape(state_vector_1, computational_basis_state_4) with pytest.raises(ValueError, match='Failed to infer'): _ = cirq.qis.infer_qid_shape(state_vector_1, state_vector_2) with pytest.raises(ValueError, match='Failed to infer'): _ = cirq.qis.infer_qid_shape(computational_basis_state_3, state_tensor_2)
def test_act_using_adaptive_two_qubit_channel(): class Decay11(cirq.Gate): def num_qubits(self) -> int: return 2 def _kraus_(self): bottom_right = cirq.one_hot(index=(3, 3), shape=(4, 4), dtype=np.complex64) top_right = cirq.one_hot(index=(0, 3), shape=(4, 4), dtype=np.complex64) return [ np.eye(4) * np.sqrt(3 / 4), (np.eye(4) - bottom_right) * np.sqrt(1 / 4), top_right * np.sqrt(1 / 4), ] mock_prng = mock.Mock() def get_result(state: np.ndarray, sample: float): mock_prng.random.return_value = sample args = cirq.ActOnStateVectorArgs( target_tensor=np.copy(state), available_buffer=np.empty_like(state), qubits=cirq.LineQubit.range(4), prng=mock_prng, log_of_measurement_results={}, ) cirq.act_on(Decay11(), args, [cirq.LineQubit(1), cirq.LineQubit(3)]) return args.target_tensor def assert_not_affected(state: np.ndarray, sample: float): np.testing.assert_allclose(get_result(state, sample), state, atol=1e-8) all_zeroes = cirq.one_hot(index=(0, 0, 0, 0), shape=(2, ) * 4, dtype=np.complex128) all_ones = cirq.one_hot(index=(1, 1, 1, 1), shape=(2, ) * 4, dtype=np.complex128) decayed_all_ones = cirq.one_hot(index=(1, 0, 1, 0), shape=(2, ) * 4, dtype=np.complex128) # Decays the 11 state to 00. np.testing.assert_allclose(get_result(all_ones, 3 / 4 - 1e-8), all_ones) np.testing.assert_allclose(get_result(all_ones, 3 / 4 + 1e-8), decayed_all_ones) # Decoheres the 11 subspace from other subspaces as sample rises. superpose = all_ones * np.sqrt(1 / 2) + all_zeroes * np.sqrt(1 / 2) np.testing.assert_allclose(get_result(superpose, 3 / 4 - 1e-8), superpose) np.testing.assert_allclose(get_result(superpose, 3 / 4 + 1e-8), all_zeroes) np.testing.assert_allclose(get_result(superpose, 7 / 8 - 1e-8), all_zeroes) np.testing.assert_allclose(get_result(superpose, 7 / 8 + 1e-8), decayed_all_ones) # Always acts like identity when sample < p=3/4. for _ in range(10): assert_not_affected( cirq.testing.random_superposition(dim=16).reshape((2, ) * 4), sample=3 / 4 - 1e-8, ) # Acts like identity on superpositions of first three states. for _ in range(10): mock_prng.random.return_value = 3 / 4 + 1e-6 projected_state = cirq.testing.random_superposition(dim=16).reshape( (2, ) * 4) projected_state[cirq.slice_for_qubits_equal_to([1, 3], 3)] = 0 projected_state /= np.linalg.norm(projected_state) assert abs(np.linalg.norm(projected_state) - 1) < 1e-8 assert_not_affected( projected_state, sample=3 / 4 + 1e-8, )
def test_fidelity_fail_inference(): state_vector = cirq.one_hot(shape=(4,), dtype=np.complex128) state_tensor = np.reshape(state_vector, (2, 2)) with pytest.raises(ValueError, match='Please specify'): _ = cirq.fidelity(state_tensor, 4)
def _channel_(self): return [ np.eye(4) - cirq.one_hot(index=(3, 3), shape=(4, 4), dtype=np.complex64), cirq.one_hot(index=(0, 3), shape=(4, 4), dtype=np.complex64), ]