Skip to content

Commit

Permalink
add initial version of observation capability, see #54
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Jan 4, 2025
1 parent c25ff4b commit 8c64ef6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
55 changes: 54 additions & 1 deletion js/bloch-sphere/model/BlochSphereModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import Multilink from '../../../../axon/js/Multilink.js';
import NumberProperty from '../../../../axon/js/NumberProperty.js';
import Property from '../../../../axon/js/Property.js';
import dotRandom from '../../../../dot/js/dotRandom.js';
import Range from '../../../../dot/js/Range.js';
import TModel from '../../../../joist/js/TModel.js';
import { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
Expand Down Expand Up @@ -55,9 +56,14 @@ export default class BlochSphereModel implements TModel {
// If is single or multiple measurement mode
public isSingleMeasurementModeProperty: BooleanProperty;

// If the model is ready to observe or needs the state to be prepared.
// A flag that indicates whether the model is ready to observe or needs the state to be prepared. This should not be
// modified directly by client code, but rather by the model's observe() and reprepare() methods.
public readonly readyToObserveProperty: BooleanProperty;

// Properties for the spin measurements made.
public readonly upMeasurementCountProperty: NumberProperty;
public readonly downMeasurementCountProperty: NumberProperty;

public constructor( providedOptions: QuantumMeasurementModelOptions ) {

this.selectedSceneProperty = new Property( BlochSphereScene.MEASUREMENT, {
Expand Down Expand Up @@ -116,6 +122,16 @@ export default class BlochSphereModel implements TModel {
phetioReadOnly: true
} );

this.upMeasurementCountProperty = new NumberProperty( 0, {
tandem: providedOptions.tandem.createTandem( 'upMeasurementCountProperty' ),
phetioReadOnly: true
} );

this.downMeasurementCountProperty = new NumberProperty( 0, {
tandem: providedOptions.tandem.createTandem( 'downMeasurementCountProperty' ),
phetioReadOnly: true
} );

let selectingStateDirection = false;
this.selectedStateDirectionProperty.link( stateDirection => {
if ( stateDirection !== StateDirection.CUSTOM ) {
Expand Down Expand Up @@ -147,6 +163,7 @@ export default class BlochSphereModel implements TModel {
}
);

// Set the precession rate of the Bloch sphere based on the magnetic field strength and the selected scene.
Multilink.multilink(
[ this.magneticFieldStrengthProperty, this.selectedSceneProperty ],
( magneticFieldStrength, selectedScene ) => {
Expand All @@ -165,10 +182,46 @@ export default class BlochSphereModel implements TModel {
} );
}

/**
* Make whatever observation ths mode is currently set up to make.
*/
public observe(): void {
if ( this.readyToObserveProperty.value ) {

// TODO: This code is a placeholder for the actual measurement logic. See https://github.com/phetsims/quantum-measurement/issues/54.
const isUp = dotRandom.nextDouble() < 0.5;
if ( isUp ) {
this.upMeasurementCountProperty.value++;
}
else {
this.downMeasurementCountProperty.value++;
}

// Update the measurement state.
this.readyToObserveProperty.value = false;
}
}

/**
* Reprepare the model for a new observation.
*/
public reprepare(): void {
this.readyToObserveProperty.value = true;
}

/**
* Resets the measurement counts.
*/
private resetCounts(): void {
this.upMeasurementCountProperty.reset();
this.downMeasurementCountProperty.reset();
}

/**
* Resets the model.
*/
public reset(): void {
this.resetCounts();
this.preparationBlochSphere.reset();
this.selectedSceneProperty.reset();
this.readyToObserveProperty.reset();
Expand Down
12 changes: 8 additions & 4 deletions js/bloch-sphere/view/BlochSphereMeasurementArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import DerivedStringProperty from '../../../../axon/js/DerivedStringProperty.js';
import NumberProperty from '../../../../axon/js/NumberProperty.js';
import { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import WithRequired from '../../../../phet-core/js/types/WithRequired.js';
import PhetFont from '../../../../scenery-phet/js/PhetFont.js';
Expand Down Expand Up @@ -73,8 +72,8 @@ export default class BlochSphereMeasurementArea extends Node {
);

const measurementResultHistogram = new QuantumMeasurementHistogram(
new NumberProperty( 1 ),
new NumberProperty( 1 ),
model.upMeasurementCountProperty,
model.downMeasurementCountProperty,
[
new RichText( spinUpLabelStringProperty ),
new RichText( spinDownLabelStringProperty ) ],
Expand Down Expand Up @@ -147,7 +146,12 @@ export default class BlochSphereMeasurementArea extends Node {
prepareObserveButtonTextProperty,
{
listener: () => {
model.readyToObserveProperty.value = !model.readyToObserveProperty.value;
if ( model.readyToObserveProperty.value ) {
model.observe();
}
else {
model.reprepare();
}
},
baseColor: QuantumMeasurementColors.experimentButtonColorProperty,
font: new PhetFont( 18 ),
Expand Down

0 comments on commit 8c64ef6

Please sign in to comment.